469,362 Members | 2,575 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

New-style objects are not instances, apparently

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?
Jan 3 '08 #1
3 1017
ee****@gmail.com wrote:
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>

Jan 3 '08 #2
On Jan 3, 9:15*pm, "eef...@gmail.com" <eef...@gmail.comwrote:
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.

--
Arnaud

Jan 3 '08 #3
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
Jan 3 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by Razvan | last post: by
5 posts views Thread by kUfa.scoopex | last post: by
2 posts views Thread by Dave | last post: by
1 post views Thread by scorpiotgs | last post: by
2 posts views Thread by jm | last post: by
3 posts views Thread by Grizlyk | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.