New-style objects are not instances, apparently

E

eefacm

I have a class that derives from Exception. In Python 2.4,
isinstance(MyClass(), types.InstanceType) was True. In 2.5, it's
False.

Further experimentation showed that derivation from object was the
culprit; new-style objects are not considered "instances" in the above
sense. I wasn't able to figure out a workaround. Is there one, or is
the distinction between traditional classes and built-in types only
going to get more and more hazy?
 
F

Fredrik Lundh

Further experimentation showed that derivation from object was the
culprit; new-style objects are not considered "instances" in the above
sense. I wasn't able to figure out a workaround. Is there one, or is
the distinction between traditional classes and built-in types only
going to get more and more hazy?

new-style classes *are* types.

one way to test for a new-style object is to compare type(obj) to
obj.__class__; if they point to the same object, it's a new-style object.

</F>
 
A

Arnaud Delobelle

I have a class that derives from Exception.  In Python 2.4,
isinstance(MyClass(), types.InstanceType) was True.  In 2.5, it's
False.

Further experimentation showed that derivation from object was the
culprit; new-style objects are not considered "instances" in the above
sense.  I wasn't able to figure out a workaround.

IIRC, this is because since 2.5 Exception is a new style class. New
style objects are instances of their class, not of InstanceType as was
the case with instances of old-style classes. So in your case
isinstance(MyClass(), Exception) will return True.
Is there one, or is
the distinction between traditional classes and built-in types only
going to get more and more hazy?

I'm not sure what you mean here.
 
M

Martin v. Löwis

Further experimentation showed that derivation from object was the
culprit; new-style objects are not considered "instances" in the above
sense. I wasn't able to figure out a workaround. Is there one, or is
the distinction between traditional classes and built-in types only
going to get more and more hazy?

In the long run (ie. Python 3), the distinction is going to be very
hazy, very dark: it will entirely disappear. There will be only one
concept of type/class, not two, so there will be no point
distinguishing between types and classes.

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top