Is 'x' an instance of a new-style class?

Hrvoje Niksic hniksic at xemacs.org
Tue Sep 16 06:05:51 EDT 2008


"Gabriel Genellina" <gagsl-py2 at yahoo.com.ar> writes:

> En Tue, 16 Sep 2008 05:26:14 -0300, MatthewS <schaefer.mp at gmail.com>
> escribió:
>
>> I've seen the question raised several times here, but apparently never
>> answered. Since PyInstance_Check returns False for new-style class
>> instances, is there a standard procedure for testing this using the C-
>> Api?
>>
>> I would greatly appreciate some help with this.
>
> In Python you would write isinstance(x, object).

It will return True for all Python objects (except perhaps those that
fiddle with metaclasses), not only for instances of new-style classes.

>>> isinstance([], object)
True
>>> isinstance((), object)
True
>>> isinstance(list, object)
True
>>> isinstance(type, object)
True

I don't think there is a good way to check if something is a new-style
class instance.  The whole point of new-style classes was removing the
distinction between classes and types.  The distinction is now so well
removed that it's hard to find it when you need it.

A better question for OP is *why* he needs to distinguish between
instances of new-style classes and other objects.



More information about the Python-list mailing list