Implicit lists

Dale Strickland-Clark dale at riverhall.NOTHANKS.co.uk
Thu Jan 30 15:15:11 EST 2003


Alex Martelli <aleax at aleax.it> wrote:

>A class is string-like if it acts like a string.  If a
>class lets its instances be concatenated to strings, it's
>pretty string-like -- all of str, unicode and UserString
>do, and no other type that's either built-in or the standard
>library.  Making this easily checked feature the single
>discriminant of "string-likeness" appears quite OK to me
>for most purposes.  You can wrap the test into highly readable
>form by the simple trick of defining a function;-)...e.g.:
>
>def isstringlike(x):
>    try: return x+'!'
>    except TypeError: return False
>

There's a bit of a language deficiency here if you have to resort to
this type of obscure trick. To test for strings, I have always coded
either of:

type(var) in (str, unicode)
isinstance(var, (str, unicode))

But, where it mattered, I have also been able to make sure that my
string-like objects are subclasses of str.

It makes you think.
--
Dale Strickland-Clark
Riverhall Systems Ltd




More information about the Python-list mailing list