Classic and New Style Classes?

C

Chris S.

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

Peter Otten

Chris said:
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:
True

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

Peter
 
L

Leif K-Brooks

Chris said:
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:

.... pass
....False
 
M

Michele Simionato

Chris S. said:
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.
True

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

Is this what you are asking for?

Michele Simionato
 
C

Chris S.

Michele said:
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.
 
C

Chris S.

Leif said:
Chris S. wrote:

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


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

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

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top