What's the difference between:
isinstance(foo, types.StringType)
and
isinstance(foo, basestring)
?
Thanks, /YGA
PS I know I shouldn't use isinstance at all, blah blah blah.
What's the difference between:
isinstance(foo, types.StringType)
and
isinstance(foo, basestring)
?
Thanks, /YGA
PS I know I shouldn't use isinstance at all, blah blah blah.
basestring is the base class for both str and unicode, while types.StringType is str. If you want to check if something is a string, use basestring. If you want to check if something is a bytestring, use str and forget about types.
This stuff is completely different in Python3
types not longer has StringType
str is always unicode
basestring no longer exists
So try not to sprinkle that stuff through your code too much if you might ever need to port it
>>> import types
>>> isinstance(u'ciao', types.StringType)
False
>>> isinstance(u'ciao', basestring)
True
>>>
Pretty important difference, it seems to me;-).