Implicit lists

Alex Martelli aleax at aleax.it
Thu Jan 30 13:12:57 EST 2003


holger krekel wrote:
   ...
> I think it's safer to skip "iteration" with an explicit
> 
>     isinstance(arg, (str, unicode))
> 
> check.  It's also simpler to read and understand.

And it breaks *EVERY* use of UserString -- a module in the
standard Python library, even!!! -- not to mention user-coded
string-like types and classes.  *shudder*.

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


Alex
 




More information about the Python-list mailing list