Type/Class Distinction

Corran Webster cwebster at nevada.edu
Sat Jun 16 11:59:28 EDT 2001


In article <9gdpbp$61k$6 at newshost.accu.uu.nl>, m.faassen at vet.uu.nl
(Martijn Faassen) wrote:

> Glyph Lefkowitz <glyph at twistedmatrix.com> wrote:
> 

[discussion about type/class split]

> 
> Right now we have to *treat* 'types' differently from 'classes'. For
> built-in objects you have to use the type() construct to check what
> you're dealing with, but for class instances you use isinstance().
> 
> Wouldn't it be nice if the language allowed something like this:
> 
> if isinstance(foo, int):
>     ...
> elif isinstance(foo, BarClass):
>     ...
> 
> (backwards compatibility issues aside)

You already can, more-or-less:

Python 2.1 (#92, Apr 24 2001, 23:59:43)  [CW CARBON GUSI2 THREADS]
>>> from types import *
>>> isinstance(5, IntType)
1
>>> isinstance(1.0, IntType)
0

> No more special casing seems good to me.

Where the PEP will help is to avoid having to do things like:

if isinstance(spam, ListType) or isinstance(spam, UserList):
    ...

and also, presumably, to help C extensions use UserLists and similar
objects interchangeably with lists.


Regards,
Corran



More information about the Python-list mailing list