"isinstance" question

J

John Nagle

I want to test whether an object is an instance of any user-defined
class. "isinstance" is less helpful than one would expect.
.... pass
....<__main__.foo instance at 0x020080A8>

So far, so good. x is an InstanceType. But let's try a
class with a constructor:
.... def __init__(self, val) :
.... self.val = val
....<class '__main__.bar'>

Without a constructor, we get an "instance". With a constructor,
we get an "object", one which is not an InstanceType.

One might think that testing for types.ObjectType would help. But
no, everything is an ObjectType:
True

So that's useless.

I have to be missing something obvious here.

(CPython 2.6)

John Nagle
 
J

John Nagle

Right. The type hierarchy is now unified; there's essentially no
difference in later Python versions between user-defined types and
built-in types.

OK, now that I know that, there's no problem.

I'n doing something that involves program analysis through
introspection. More on this later.

John Nagle
 
G

Gabriel Genellina

I want to test whether an object is an instance of any user-defined
class. "isinstance" is less helpful than one would expect.

... pass
...
<__main__.foo instance at 0x020080A8>

So far, so good. x is an InstanceType. But let's try a
class with a constructor:

... def __init__(self, val) :
... self.val = val
...
<class '__main__.bar'>

Without a constructor, we get an "instance". With a constructor,
we get an "object", one which is not an InstanceType.

That's not the relevant difference. In the first case, you don't inherit
from object; in the second one, you do.

foo is a "classic" class (or "old-style" class); x is an instance of foo,
its *type* is InstanceType, its *class* is foo. All instances of any other
classic class have the same type (InstanceType).

bar is a "new-style" class, b is an instance of bar, its type is bar, its
class is bar. class and type are equivalent for new style classes; things
are a lot more regular and predictable. In Python 3.x classic classes are
gone.
 
T

Thomas Jollans

I want to test whether an object is an instance of any
user-defined
class. "isinstance" is less helpful than one would expect.

... pass
...
<__main__.foo instance at 0x020080A8>

So far, so good. x is an InstanceType. But let's try a
class with a constructor:

... def __init__(self, val) :
... self.val = val
...
<class '__main__.bar'>

well the same code on my side returns true when you run isinstance(b,
types.InstanceType) even when the class has a constructor. Why is there
a difference in the output when we are both using Cython 2.6 ?? (2.6.4
to be exact)

I assume you mean CPython, not Cython.

As Gabriel said, the difference is that bar is a subclass of object. If
isinstance(bar(100), types.InstanceType), then you did not subclass
object, and get an old-style class.
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,126
Latest member
FastBurnketoIngredients
Top