"my brain hurts" or is isinstance broken?

Walter Dörwald walter at livinglogic.de
Wed Jul 3 07:59:18 EDT 2002


Robert Kuzelj wrote:
> 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!

That you can't distinguish between types and classes. Every
object is an instance of it's type in 2.2 (at least for new style
objects/classes). Even a class object is an instance of it's
type.

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

I think it would be helpful, if you descripe what problem
you're trying to solve.

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

Yes, but "type checks are evil".

> i think
> i have read this somewhere on the python pages? didnt i? ;-)

Right, do an "import this"! ;)

> 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.

Every object foo was created by a call to foo.__class__.__new__()
and initialized by foo.__class__.__init__().

Bye,
    Walter Dörwald






More information about the Python-list mailing list