473,473 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Classic and New Style Classes?

I'm generating the source of an object from a list of objects. Therefore
I need to determine if the object is a class definition (i.e. 'def
something(whatever):') or a class instance (i.e. 'somename =
somethingelse()'). With classic classes this is trivial, since all I
have to do is check the type. However, for the new "improved" style
classes this seems next to impossible, since everything is essentially
an instance of something.

isinstance(class, classinfo) won't work since I won't necessarily know
classinfo.

Is there any way to make this distinction? Any help is appreciated.
Jul 18 '05 #1
5 1512
Chris S. wrote:
I'm generating the source of an object from a list of objects. Therefore
I need to determine if the object is a class definition (i.e. 'def
something(whatever):') or a class instance (i.e. 'somename =
somethingelse()'). With classic classes this is trivial, since all I
have to do is check the type. However, for the new "improved" style
classes this seems next to impossible, since everything is essentially
an instance of something.

isinstance(class, classinfo) won't work since I won't necessarily know
classinfo.

Is there any way to make this distinction? Any help is appreciated.


It's not clear to me why isinstance() wouldn't work:
import types
def f(): pass .... class T(object): pass .... class U: pass .... isinstance(T(), types.FunctionType) False isinstance(U(), types.FunctionType) False isinstance(f, types.FunctionType) True

Another option would be to use callable(), which also returns True for
classes and callable instances:
class V(object): .... def __call__(self): pass
.... callable(T) True callable(T()) False callable(V())

True

Peter

Jul 18 '05 #2
Chris S. wrote:
I'm generating the source of an object from a list of objects. Therefore
I need to determine if the object is a class definition (i.e. 'def
something(whatever):') or a class instance (i.e. 'somename =
somethingelse()').


I'm assuming you meant 'class something(whatever):' rather than def. If
so, this should work:

class Foo(object): .... pass
.... isinstance(Foo, type) True isinstance(Foo(), type)

False
Jul 18 '05 #3
"Chris S." <ch*****@NOSPAMudel.edu> wrote in message news:<cb**********@scrotar.nss.udel.edu>...
I'm generating the source of an object from a list of objects. Therefore
I need to determine if the object is a class definition (i.e. 'def
something(whatever):') or a class instance (i.e. 'somename =
somethingelse()'). With classic classes this is trivial, since all I
have to do is check the type. However, for the new "improved" style
classes this seems next to impossible, since everything is essentially
an instance of something.

isinstance(class, classinfo) won't work since I won't necessarily know
classinfo.

Is there any way to make this distinction? Any help is appreciated.

from types import ClassType
class Old: pass .... isinstance(Old, ClassType) True class New(object): pass .... isinstance(New, type)

True

Also Old.__class__ raises an error whereas New.__class__ returns type(New).

Is this what you are asking for?

Michele Simionato
Jul 18 '05 #4
Michele Simionato wrote:
Also Old.__class__ raises an error whereas New.__class__ returns type(New).

Is this what you are asking for?

Michele Simionato


Yes, thanks a lot.
Jul 18 '05 #5
Leif K-Brooks wrote:
Chris S. wrote:

I'm assuming you meant 'class something(whatever):' rather than def. If
so, this should work:

>>> class Foo(object): ... pass
... >>> isinstance(Foo, type) True >>> isinstance(Foo(), type)

False


Yeah, minor typo. I needed to differentiate a class definition from a
class instance, and now I see using isinstance(obj,type) does the trick.
Thanks a lot.
Jul 18 '05 #6

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

Similar topics

1
by: Skip Montanaro | last post by:
I just stumbled upon a bug in some group-written code. We have this sort of class hierarchy: class X(object): ... class A(X): def __init__(...): self.attr = 0.0
0
by: Skip Montanaro | last post by:
If I have a new-style class what are the restrictions that keep me from going back to a classic class? I know use of __slots__, __new__ or the property() builtin make new-style a requirement. ...
16
by: Simon Wittber | last post by:
I've noticed that a few ASPN cookbook recipes, which are recent additions, use classic classes. I've also noticed classic classes are used in many places in the standard library. I've been...
3
by: ShepardBerry | last post by:
I've been working in .NET for some time now and I don't remember specifically how asp classes are cleaned up in classic asp. I've been put on a Classic ASP project(ugh) and we're having some...
5
by: venk | last post by:
Hi, can some one properly explain the differences between class types and classic classes? ... Still face problems in identifying what is what.
1
by: ankit | last post by:
Hello, Please put some light on, What are new style classes and classic style classes in python. The basic differences in them. And How can I decide to choose one.
1
by: Kay Schluehr | last post by:
Just reasoning about conversion of classic to new style classes ( keeping deprecation of ClCl in Py3K in mind ) I wonder if there is a metaclass that can be used to express the semantics of ClCl in...
26
by: momobear | last post by:
hi, I am puzzled about how to determine whether an object is initilized in one class, anyone could give me any instructions? here is an example code: class coffee: def boil(self): self.temp =...
4
by: Quek | last post by:
Hi all, I'm really new to Python and I've been reading up some texts on older versions of Python (2.2 to be specific). The text briefly mentioned new style and classic classes. I'd really...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
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
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...
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.