469,923 Members | 1,584 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

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 1396
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Skip Montanaro | last post: by
16 posts views Thread by Simon Wittber | last post: by
3 posts views Thread by ShepardBerry | last post: by
1 post views Thread by ankit | last post: by
1 post views Thread by Kay Schluehr | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.