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(whate ver):') 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(clas s, classinfo) won't work since I won't necessarily know
classinfo.
Is there any way to make this distinction? Any help is appreciated. 5 1526
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(whate ver):') 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(clas s, 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.FunctionT ype)
False isinstance(U(), types.FunctionT ype)
False isinstance(f, types.FunctionT ype)
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
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(whate ver):') or a class instance (i.e. 'somename = somethingelse() ').
I'm assuming you meant 'class something(whate ver):' rather than def. If
so, this should work: class Foo(object):
.... pass
.... isinstance(Foo, type)
True isinstance(Foo( ), type)
False
"Chris S." <ch*****@NOSPAM udel.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(whate ver):') 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(clas s, 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
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.
Leif K-Brooks wrote: Chris S. wrote:
I'm assuming you meant 'class something(whate ver):' 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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. What other constructs
might the code be using that would prevent me from falling back to use of
classic classes?
Thx,
Skip
|
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 using new-style classes since Python 2.2, and am suprised
people are still using the classic classes.
Is there a legitimate use for classic classes that I am not aware of?
|
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 serious stability problems. Once we
reach a point of sustained CPU useage of over 80% IIS 6 restarts the w3wp.exe
process which of course terminates all sessions and resets the website. I've
noticed that the previous developer has an include which...
|
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.
| |
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.
|
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 terms of
new style classes? Intuitively I would expect that there is quite a lot
of Python semantics that could be expressed in pure Python - of course
without circular definitions.
|
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 = 80
a = coffer()
if a.temp 60:
|
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 like to know in the current context of Python 2.5, besides
in the cases of multi-inheritance, where would I use new style
classes? Is it a norm to use more new style classes even if I don't
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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...
| |
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...
|
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...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |