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

Maric Michaud maric at aristote.info
Tue Sep 16 08:03:31 EDT 2008


Le Tuesday 16 September 2008 12:05:51 Hrvoje Niksic, vous avez écrit :
> "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.
>

Yes, everything (including instances of old style classes) are instance of a 
new style class but :

not isinstance(x, types.InstanceType)

ensure you that x is not an instance of old style class.

> A better question for OP is *why* he needs to distinguish between
> instances of new-style classes and other objects.
> --
> http://mail.python.org/mailman/listinfo/python-list



-- 
_____________

Maric Michaud



More information about the Python-list mailing list