473,396 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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

Sep 16 '08 #1
6 1967
En Tue, 16 Sep 2008 05:26:14 -0300, MatthewS <sc*********@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). 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)

--
Gabriel Genellina

Sep 16 '08 #2
"Gabriel Genellina" <ga*******@yahoo.com.arwrites:
En Tue, 16 Sep 2008 05:26:14 -0300, MatthewS <sc*********@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.
Sep 16 '08 #3
Le Tuesday 16 September 2008 12:05:51 Hrvoje Niksic, vous avez écrit*:
"Gabriel Genellina" <ga*******@yahoo.com.arwrites:
En Tue, 16 Sep 2008 05:26:14 -0300, MatthewS <sc*********@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
Sep 16 '08 #4
On Sep 16, 11:38*am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Tue, 16 Sep 2008 05:26:14 -0300, MatthewS <schaefer...@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). 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)

--
Gabriel Genellina
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;
}
}

Sep 16 '08 #5
MatthewS <sc*********@gmail.comwrites:
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;
}
}
I think this will return TRUE on any Python object you pass it.
Sep 16 '08 #6
On Sep 16, 8:05 pm, Hrvoje Niksic <hnik...@xemacs.orgwrote:
"Gabriel Genellina" <gagsl-...@yahoo.com.arwrites:
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.
Unlike say isinstance(x.__class__, type) which should distinguish
between instances of new and old-style classes.
Sep 17 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: cat | last post by:
HiFolks, I am newbie in DB2 ESE. Here is the question, I already created an instance db2inst1 under file system /db2home/db2inst1 ( actually the instance has been created after installation.)...
2
by: ari_weil | last post by:
Can it be that when I'm running DB2 8.1 on AIX 5.2 I have to create a new user every time I want to create a new instance? I tried the standard db2icrt command using a different instance name...
6
by: Hemant Shah | last post by:
Folks, I need to move HOME directory of an instance to another directory. What is the best way of doing it? Is changing password file enough? or dies DB2 store this info in it's own config? ...
0
by: Joe Harrison | last post by:
Hello. I have an application which I check to see if there is another running instance at startup. If there is another running instance, I set focus to the existing instance. I am using code...
5
by: Alexander Inochkin | last post by:
Hello. I have a working ASP.NET service. I am using .NET Framework 1.1; IIS V6.0. (web garden not used) When I change web.config file or load new *.dll, my service restarts, and about 90...
6
by: Shane Saunders | last post by:
I have a menu option that loads a form and displays infomation. if you go to main form and try to load something else from that same menu, it does a check to see if the form in exist and then...
18
by: Daniel | last post by:
Hey guys I have an instance of an object say: List<Object> myList = new List<Object>(); Object myObject = new Object(); myObject.PositionVector = new Vector3(10,10,10); ...
0
by: Miro | last post by:
I searched google and I think I have solved the issue. Again, if someone could quickly skim over my example and lemmi know if it is the "correct" way of doing things. It works, but again - is it...
4
by: datapro01 | last post by:
I am running DB2 LUW 8.2.5 on AIX 5.3 I am being asked to rename an instance in Production which contains several databases. I am being told it would be difficult to shut the instance down. ...
4
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.