where are isinstance types documented?

John Machin sjmachin at lexicon.net
Tue Sep 26 06:28:03 EDT 2006


codefire wrote:
> Hi,
>
> I'm using the isinstance built-in function. I've found the docs for it,
> but there are no docs on the supported types.

It supports *all* types.

>
> For example isinstance(a, int) works fine but isinstance(s, string)
> doesn't - because 'string is not known'.

That's because there is no built-in type called "string".

>
> I do know how to import the types module and then use defined types
> such as 'types.StringType' - but the documentation says that from 2.2
> this is not the preferred way.
>
> So, where's the documentation for types I can use with isinstance, such
> as 'int'?

This is must-read stuff, but doesn't give the actual type names:
    http://docs.python.org/ref/types.html
What you are looking for is this:
    http://docs.python.org/lib/types.html
which is also must-read, but you may find it faster to use the "Who's
your father?" technique on a known instance:

| >>> type([])
| <type 'list'>
| >>> type('')
| <type 'str'>
| >>> type(u'')
| <type 'unicode'>
| >>> isinstance('foo', str)
| True
| >>> isinstance('foo', unicode)
| False

"Who's your grandfather?" can be useful, too:

| >>> str.__mro__
| (<type 'str'>, <type 'basestring'>, <type 'object'>)
| >>> isinstance('foo', basestring)
| True
| >>> isinstance(u'u2', basestring)
| True

HTH,
John




More information about the Python-list mailing list