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

M

MatthewS

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.

/Matthew
 
G

Gabriel Genellina

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). In C, "object" is
PyBaseObject_Type, and a direct translation would be
PyObject_IsInstance(x, PyBaseObject_Type), or perhaps
PyObject_TypeCheck(x, &PyBaseObject_Type) (to mimic how other PyXXX_Check
are implemented, and probably faster)
 
M

Maric Michaud

Le Tuesday 16 September 2008 12:05:51 Hrvoje Niksic, vous avez écrit :
Gabriel Genellina said:
En Tue, 16 Sep 2008 05:26:14 -0300, MatthewS <[email protected]>

escribió:

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.
 
M

MatthewS

In Python you would write isinstance(x, object). In C, "object" is  
PyBaseObject_Type, and a direct translation would be  
PyObject_IsInstance(x, PyBaseObject_Type), or perhaps  
PyObject_TypeCheck(x,  &PyBaseObject_Type) (to mimic how other PyXXX_Check  
are implemented, and probably faster)

Thanks Gabriel, the second solution seems to work based on my tests.
So I have this now this test:

static BOOL IsPythonInstance( PyObject * src )
{
{
// old style classes or instances or new style classes or instances
if( ( PyInstance_Check( src ) ) || ( PyClass_Check( src ) ) ||
PyObject_TypeCheck( src, &PyBaseObject_Type) )
{
return TRUE;
}

return FALSE;
}
}
 
A

Asun Friere

It will return True for all Python objects (except perhaps those that
fiddle with metaclasses), not only for instances of new-style classes.
Unlike say isinstance(x.__class__, type) which should distinguish
between instances of new and old-style classes.
 

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

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top