By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,547 Members | 1,417 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,547 IT Pros & Developers. It's quick & easy.

Determining if an object is a class?

P: n/a
I need to find out if an object is a class. Using new style classes
this is very easy:

class Test(object): pass

obj = Test
or
obj = Test()

if type(obj) == type:
# this is a class object..
else:
# this not a class object

But this fails for old style classes. For example:

class OldStyleObject: pass
>>type(OldStyleObject)
<type 'classobj'>

But I have no way of comparing the return value to anything:
>>type(OldStyleObject) == classobj
Error, classobj not defined!

Since this is all duck typed, I have no idea what the class will be
passed in as, so I can't just compare the raw object. I have a
solution for this:

if str(type(Test)).find('classobj') != -1:
# this is a class object

Which is quite simply awful...does anyone know of a better way to do
this?

Jul 12 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
>>import types
>>class OldStyle: pass
....
>>type(OldStyle) == types.ClassType
True

Jul 12 '06 #2

P: n/a
Ahh much better. Thanks.

Jul 12 '06 #3

P: n/a
The first check is also off - it should if issubclass(type(Test), type): otherwise you miss the metaclass case:

class foo(type): pass

class Test(object):
__metaclass__ = foo

obj = Test
if type(obj) == type: 'class obj'
else: 'not a class'

just on the off-chance you run into a metaclass :)

-----Original Message-----
From: py*************************************@python.org [mailto:py*************************************@pyt hon.org] On Behalf Of Clay Culver
Sent: Wednesday, July 12, 2006 2:07 PM
To: py*********@python.org
Subject: Re: Determining if an object is a class?

Ahh much better. Thanks.

--
http://mail.python.org/mailman/listinfo/python-list
Jul 13 '06 #4

P: n/a

Cl*********@yahoo.com wrote:
I need to find out if an object is a class. Using new style classes
this is very easy:

class Test(object): pass

obj = Test
or
obj = Test()

if type(obj) == type:
# this is a class object..
else:
# this not a class object

But this fails for old style classes. For example:

class OldStyleObject: pass
Why is there old and new classes? What are the differences?

--Cheers

Jul 13 '06 #5

P: n/a
Cl*********@yahoo.com wrote:
I need to find out if an object is a class.
Which is quite simply awful...does anyone know of a better way to do
this?
inspect.isclass

M.S.

Jul 13 '06 #6

P: n/a
Clay,
Search Google for it. Here is one good explanation with code examples
by Michael Fotsch:
http://www.geocities.com/foetsch/pyt...le_classes.htm
Nick V.
placid wrote:
Cl*********@yahoo.com wrote:
I need to find out if an object is a class. Using new style classes
this is very easy:

class Test(object): pass

obj = Test
or
obj = Test()

if type(obj) == type:
# this is a class object..
else:
# this not a class object

But this fails for old style classes. For example:

class OldStyleObject: pass

Why is there old and new classes? What are the differences?

--Cheers
Jul 13 '06 #7

P: n/a
Michele Simionato wrote:
Cl*********@yahoo.com wrote:
>I need to find out if an object is a class.
Which is quite simply awful...does anyone know of a better way to do
this?

inspect.isclass

M.S.
....which made me wonder what this canonical test is. The answer:

def isclass(object):
"""Return true if the object is a class.

Class objects provide these attributes:
__doc__ documentation string
__module__ name of module in which this class was defined"""
return isinstance(object, types.ClassType) or hasattr(object, '__bases__')

I wonder why the test isn't

isinstance(object, (types.ClassType, type))

Michael

Jul 13 '06 #8

P: n/a
placid wrote:
Why is there old and new classes? What are the differences?
the "what's new" document for the release where they were introduced
(2.2) contains a nice overview, written from a "before and after"
perspective:

http://www.python.org/doc/2.2.3/what...-rellinks.html

(however, the exact details of the MRO algorithm was changed in 2.3; see
http://www.python.org/download/releases/2.3/mro for the full story)

</F>

Jul 13 '06 #9

P: n/a
Dino Viehland wrote:
The first check is also off - it should if issubclass(type(Test), type): otherwise you miss the metaclass case:

class foo(type): pass

class Test(object):
__metaclass__ = foo

obj = Test
if type(obj) == type: 'class obj'
else: 'not a class'

just on the off-chance you run into a metaclass :)
Ah right...it's so easy to forget about metaclassing, despite how easy
Python makes it. Good catch, thanks.

Jul 13 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.