How to return an "not string' error in function?

Duncan Booth duncan.booth at invalid.invalid
Thu Sep 21 11:09:41 EDT 2006


Tim Chase <python.list at tim.thechases.com> wrote:

> This will return true for both regular strings and for unicode 
> strings.  If that's a problem, you can use
> 
> >>> import types
> >>> isinstance("hello", types.StringType)
> True
> >>> isinstance(u"hello", types.StringType)
> False
> >>> isinstance("hello", types.UnicodeType)
> False
> >>> isinstance(u"hello", types.UnicodeType)
> True
> 
> ...or, if you don't want to qualify them with "types." each time, 
> you can use
> 
> >>> from types import StringType, UnicodeType
> 
> to bring them into the local namespace.

They already are in the builtin namespace under their more usual names of 
str and unicode respectively, so there is no need to import them, just use 
them:

>>> isinstance("hello", str)
True

... etc ...



More information about the Python-list mailing list