where are isinstance types documented?

C

codefire

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.

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

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'?

Many thanks,
Tony
 
S

Sybren Stuvel

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

All types/classes are supported.
For example isinstance(a, int) works fine but isinstance(s, string)
doesn't - because 'string is not known'.

That is because 'string' is not a class:

print "abc".__class__.__name__

The name is 'str', not 'string'.

Sybren
 
S

Simon Brunning

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

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

In this case, you want "str" rather than "string".

I can't find a single page with a list of built-in types (which
doesn't mean that one doesn't exist) but I think you can find them all
hanging off this page: <http://docs.python.org/lib/types.html>.
 
F

Fredrik Lundh

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

*all* types and classes can be used.
For example isinstance(a, int) works fine but isinstance(s, string)
doesn't - because 'string is not known'.

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'?

based on http://docs.python.org/ref/types.html, here's a list of the
most commonly used core types:

None:
(singleton; use "is None" to test for this)

Numbers:
int
long
bool
float
complex

Sequences:
str
unicode
tuple
list

Mappings:
dict

Class Types:
(any class defined using "class")

Files:
file

hope this helps!

</F>
 
C

codefire

OK Simon, thanks for that link, I think I can ferret out the common
types from there.

Tony
 
J

John Machin

codefire said:
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
 
F

Fredrik Lundh

Fredrik said:
Sequences:
str
unicode

footnote: to simplify, there's also a "basestring" base class that can
be used to check for either str or unicode:

isinstance(obj, basestring)

is equivalent to

isinstance(obj, (str, unicode))

</F>
 
S

Simon Brunning

In this case, you want "str" rather than "string".

A couple of points to consider:

Firstly, do you really need to use isinstance() at all? It's often not
necessary. Consider a duck-typing approach instead - i.e. to just try
using the object as a string, and to deal with exceptions arising from
the possibility that it isn't. This approach would allow string-like
objects as well as "real" strings.

Secondly, if you *really* want to use isinstance(), "basestring" is
the super-type of both "str" and "unicode", so it's more likely to be
what you want.
 
D

Duncan Booth

Fredrik Lundh said:
Sequences:
str
unicode
tuple
list

It is also worth mentioning that you can use "isinstance(a, basestring)" as
a way to check for either string type although, of course, "isinstance(a,
(str, unicode))" also works.

So far as I know there is no practical use for basestring other than using
it in isinstance.
 
G

Georg Brandl

Fredrik said:
Sion said:
based on http://docs.python.org/ref/types.html, here's a list of the
most commonly used core types:
[ ... ]
Sequences:
str
unicode
tuple
list

And set, presumably.

absolutely!

however, sets don't seem to be mentioned on that page at all (and if
they were, they should be under mappings, right?). any documentation
hackers out there ?

Filed a doc bug (#1565919). Should be a piece of cake for someone more
versed in the English language than me ;)

Georg
 
J

John Machin

Georg said:
Fredrik said:
based on http://docs.python.org/ref/types.html, here's a list of the
most commonly used core types:
[ ... ]
Sequences:
str
unicode
tuple
list

And set, presumably.

absolutely!

however, sets don't seem to be mentioned on that page at all (and if
they were, they should be under mappings, right?). any documentation
hackers out there ?

Filed a doc bug (#1565919). Should be a piece of cake for someone more
versed in the English language than me ;)

and frozenset, presumably.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top