How to tell the difference between string and list

John J. Lee jjl at pobox.com
Fri Dec 5 12:03:35 EST 2003


Jan Kokoska <kokoska.jan at globe.cz> writes:

> I need to recognize 'var' and ['var'], usually I would use:
[...]

All the other solutions posted are bad because they fail for
user-defined string-like classes (unless those classes use the new 2.2
features by deriving from str or unicode).  As long as your strings
aren't huge:

def isstringlike(x):
    try: x+""
    except TypeError: return False
    else: return True


I think I stole this off Alex Martelli.


John




More information about the Python-list mailing list