[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

New-style objects are not instances, apparently

Sean McAfee

1/3/2008 9:15:00 PM

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

Fredrik Lundh

1/3/2008 9:31:00 PM

0

eefacm@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>

Arnaud Delobelle

1/3/2008 9:31:00 PM

0

On Jan 3, 9:15 pm, "eef...@gmail.com" <eef...@gmail.com> wrote:
> 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

Martin v. Loewis

1/3/2008 9:47:00 PM

0

> 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