tags:

views:

77

answers:

3

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.

+2  A: 

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.

Lukáš Lalinský
+2  A: 

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

gnibbler
+1  A: 
>>> import types
>>> isinstance(u'ciao', types.StringType)
False
>>> isinstance(u'ciao', basestring)
True
>>>

Pretty important difference, it seems to me;-).

Alex Martelli