"my brain hurts" or is isinstance broken?

Robert Kuzelj robert_kuzelj at yahoo.com
Wed Jul 3 02:41:53 EDT 2002


hallo walter,

thanks for the reply.

>> class metatype(type): pass
>> 
>> class A1: pass
>> class B1(object): pass
>> class C1: __metaclass__ = metatype
>>
 
>This is not a bug, it's one of the main features of new style
>classes.
what is a feature of 2.2? that you cant distinguish between
a class and an instance? hardly!

>> of what type are B1() and C1()?
> B1 and C1.
i am very well aware of this. but it doesnt solve my problem.

because
C1.__class__ =~ metatype
C1().__class__ =~ C1

there is no _direct_ possebility to find out if
a given object is an instance or a class (new style one that is).

well there is a workaround:

if not isinstance(obj, types.ClassType) and \
   not isinstance(obj, types.TypeType):
   print "its an instance!"

but isn't explicit better than implicit? i think
i have read this somewhere on the python pages? didnt i? ;-)

imo it should be very clear that there is a difference between
the following two statements:
class A(object): #defines a class which is an object of type type.
A() # creates an instance

and the information on how the object was created should be explictly
accessibly.

ciao robertj



More information about the Python-list mailing list