How to tell if function was passed a list or a string?

Daniel Nogradi nogradi at gmail.com
Thu May 18 04:27:06 EDT 2006


> > >>> import types
> > >>> type("") is types.ListType
> > False
> > >>> type("") is types.StringType
> > True
> > >>> type([]) is types.StringType
> > False
> > >>> type([]) is types.ListType
> > True
>
> This is even worse than an 'isinstance' check; it refuses even
> subclasses of the types you accept, breaking polymorphism *and*
> inheritance.

And also note the following comment in the types module of the
standard library that was imported above:

# StringTypes is already outdated.  Instead of writing "type(x) in
# types.StringTypes", you should use "isinstance(x, basestring)".  But
# we keep around for compatibility with Python 2.2.



More information about the Python-list mailing list