473,786 Members | 2,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

I've seen the question raised several times here, but apparently never
answered. Since PyInstance_Chec k 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 1995
En Tue, 16 Sep 2008 05:26:14 -0300, MatthewS <sc*********@gm ail.com>
escribió:
I've seen the question raised several times here, but apparently never
answered. Since PyInstance_Chec k 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_Ty pe, and a direct translation would be
PyObject_IsInst ance(x, PyBaseObject_Ty pe), or perhaps
PyObject_TypeCh eck(x, &PyBaseObject_T ype) (to mimic how other PyXXX_Check
are implemented, and probably faster)

--
Gabriel Genellina

Sep 16 '08 #2
"Gabriel Genellina" <ga*******@yaho o.com.arwrites:
En Tue, 16 Sep 2008 05:26:14 -0300, MatthewS <sc*********@gm ail.com>
escribió:
>I've seen the question raised several times here, but apparently never
answered. Since PyInstance_Chec k 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(li st, object)
True
>>isinstance(ty pe, 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*******@yaho o.com.arwrites:
En Tue, 16 Sep 2008 05:26:14 -0300, MatthewS <sc*********@gm ail.com>

escribió:
I've seen the question raised several times here, but apparently never
answered. Since PyInstance_Chec k 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(lis t, object)

True
>isinstance(typ e, 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.InstanceT ype)

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.a r>
wrote:
En Tue, 16 Sep 2008 05:26:14 -0300, MatthewS <schaefer...@gm ail.com*
escribió:
I've seen the question raised several times here, but apparently never
answered. Since PyInstance_Chec k 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_Ty pe, and a direct translation would be *
PyObject_IsInst ance(x, PyBaseObject_Ty pe), or perhaps *
PyObject_TypeCh eck(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 IsPythonInstanc e( PyObject * src )
{
{
// old style classes or instances or new style classes or instances
if( ( PyInstance_Chec k( src ) ) || ( PyClass_Check( src ) ) ||
PyObject_TypeCh eck( src, &PyBaseObject_T ype) )
{
return TRUE;
}

return FALSE;
}
}

Sep 16 '08 #5
MatthewS <sc*********@gm ail.comwrites:
Thanks Gabriel, the second solution seems to work based on my tests.
So I have this now this test:

static BOOL IsPythonInstanc e( PyObject * src )
{
{
// old style classes or instances or new style classes or instances
if( ( PyInstance_Chec k( src ) ) || ( PyClass_Check( src ) ) ||
PyObject_TypeCh eck( src, &PyBaseObject_T ype) )
{
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.a rwrites:
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
1957
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.) Now I want to create an additional instance which the DB2PATH is point to /aix1/db2test. What shall I do, I found in IBM doc says You can change the location of the instance directory from DB2PATH using
2
3601
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 and it errors out unless I have a user of the same name on the system. I hate to say the thing I know most DBAs and especially Unix gurus hate hearing, but Windows and the AS/400 don't make me do this...clearly I'm new to Unix/Linux. Any help...
6
8928
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? I am running UDB 8.2 on Linux and AIX. Thanks.
0
1493
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 that I found on a webpage to do this (Code I am using is at the bottom of this post). When the application is minimized, the form is hidden and an icon appears in the System Tray. My problem is that when I run another instance, the new instance...
5
1763
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 seconds two versions(instances) of my application exists. Is anybody know how to minimize the time between starting of the new version(instance) of application and the application end of previous
6
1436
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 reloads it has to or does messagebox. the problem i have is that i want to set the module instance varible to nothing when i exit the form that is being check. The code i am use is If frmWeatherMap Is Nothing Then
18
1759
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); myList.Add(myObject);
0
1171
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 the right way - or have I not thought of something down the road. So from what I can understand...If i want ever one instance of a form, - and that form can be called from any child form, any showmodal form, basically from anywhere in the...
4
2148
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. My thoughts would be to create a new instance then do an export/import on the databases into the new instance. Any thoughts on the best way to go about this would be appreciated.
4
4531
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 child apps of an IIS application and can be used by multiple users during the application life cycle and for multiple page loads for the same or different page under a root application. What I don't understand and need to know is whether that...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8992
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.