"my brain hurts" or is isinstance broken?

Walter Dörwald walter at livinglogic.de
Tue Jul 2 18:19:51 EDT 2002


Robert Kuzelj wrote:
> hi,
> 
> after playing around with all this metastuff it seems
> that isinstance is broken (but after all it could also be, 
> that my brain finally broke down).
> 
> here some script that checks for the instancetype of various
> objects defined within that script.
> 
> 
>>>>typestest.py
>>>
> import types
> import string
> 
> class metatype(type): pass
> 
> class A1: pass
> class B1(object): pass
> class C1: __metaclass__ = metatype
> 
> values = [  [A1, A1()],
>             [B1, B1()],
>             [C1, C1()]]
> 
> def getInstanceInfos(row):
>     return [row[0].__name__,
>             str(isinstance(row[0], types.InstanceType)),
>             str(isinstance(row[0], types.ClassType)), 
>             str(isinstance(row[0], types.TypeType)),
>             str(isinstance(row[1], types.InstanceType)),
>             str(isinstance(row[1], types.ClassType)),
>             str(isinstance(row[1], types.TypeType))]
> 
> print 
> print "INSTANCEINFOS"
> print "  name | inst | class | type  | inst  | class | type  "
> print "-----------------------------------------------------"
> for row in values:
>     print "  " + string.join(getInstanceInfos(row), "   |   ")
> 
>>>>typestest.py #end
>>>
> 
>>>>output
>>>
> INSTANCEINFOS
>   name | inst | class | type  | inst  | class | type
> -----------------------------------------------------
>   A1   |   0   |   1   |   0   |   1   |   0   |   0
>   B1   |   0   |   0   |   1   |   0   |   0   |   0
>   C1   |   0   |   0   |   1   |   0   |   0   |   0
> 
>>>>output
>>>
> 
> my question is why are the B1() and C1() not types.InstanceType?

That's because types.InstancesType is the type of
old style instances (and types.ClassType is the type of
old style classes). The type of new style instances is
their class. That's what new style classes were made
for.

> and if this is not a bug 

This is not a bug, it's one of the main features of new style
classes.

> of what type are B1() and C1()?

B1 and C1.

Bye,
    Walter Dörwald






More information about the Python-list mailing list