473,320 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

where are isinstance types documented?

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

Sep 26 '06 #1
12 1811
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
--
Sybren Stüvel
Stüvel IT - http://www.stuvel.eu/
Sep 26 '06 #2
On 26 Sep 2006 02:59:07 -0700, codefire <to**********@gmail.comwrote:
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>.

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Sep 26 '06 #3

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

Tony

Sep 26 '06 #4
codefire wrote:
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>

Sep 26 '06 #5
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

Sep 26 '06 #6
Fredrik Lundh wrote:
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>

Sep 26 '06 #7
On 9/26/06, Simon Brunning <si***@brunningonline.netwrote:
On 26 Sep 2006 02:59:07 -0700, codefire <to**********@gmail.comwrote:
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".
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.

--
Cheers,
Simon B,
si***@brunningonline.net
Sep 26 '06 #8
Fredrik Lundh <fr*****@pythonware.comwrote:
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.
Sep 26 '06 #9
Fredrik Lundh <fr*****@pythonware.comwrote:
>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.

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
Sep 26 '06 #10
Sion Arrowsmith wrote:
>
>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 ?

</F>

Sep 26 '06 #11
Fredrik Lundh wrote:
Sion Arrowsmith wrote:
>>
>>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
Sep 26 '06 #12

Georg Brandl wrote:
Fredrik Lundh wrote:
Sion Arrowsmith wrote:
>
>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.

Sep 26 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Micha³ Wo¼niak | last post by:
Ok, I have a nice iface, I have a (hopefully) good user authentication, I have a DB up-and-running, and I have PHP 4.3.4 (yeah, I know, I will update...). What I need is a logging utility. And...
6
by: Michal Vitecek | last post by:
hello, please consider the following situation: under the current directory there's a subdirectory 'package' with two files: __init__.py and module.py ./package: __init__.py module.py
17
by: Grant Edwards | last post by:
I'm trying to figure out how to sort a list, and I've run into a problem that that I have tripped over constantly for years: where are the methods of basic types documented? The only thing I can...
5
by: Jordan Rastrick | last post by:
Hi everyone, Just a little issue that I've come across in Python where I'd be interested to read the thoughts and opinions of the great thinkers and programmers who frequent this newsgroup. ...
1
by: louis_la_brocante | last post by:
Dear all, I am having trouble generating a client proxy for a webservice whose methods return a "complex" type. The type is complex in that it is a class whose members are a mix of primitive...
21
by: abcd | last post by:
In my code I am debating whether or not to validate the types of data being passed to my functions. For example def sayHello(self, name): if not name: rasie "name can't be null" if not...
7
by: Tlis | last post by:
I am using a software system with an embedded Python interpreter (version 2.3) for scripting. The KcsPoint2D.py module contains a Point2D class with the following method: def...
6
by: HillBilly | last post by:
One question I have not figured out is how to learn which event parameter list is supported by a control? EventArgs, CommandEventArgs, what?
5
by: Boris Borcic | last post by:
Given the ABC innovation, maybe an infix syntax for isinstance() would be good. Possibilities : - stealing "is" away from object identity. As a motivation, true use cases for testing object...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.