[EMAIL PROTECTED] 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
[EMAIL PROTECTED] 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.
--
http://mail.python.org/mailman/listinfo/python-list
Clay,
Search Google for it. Here is one good explanation with code examples
by Michael Fotsch:
http://www.geocities.com/foetsch/python/new_style_classes.htm
Nick V.
placid wrote:
[EMAIL PROTECTED] wrote:
I need to find out if an object is a class. Using new style classes
this is very easy:
Michele Simionato wrote:
[EMAIL PROTECTED] 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):
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/whatsnew/sect-rellinks.html
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
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
import types
class OldStyle: pass
...
type(OldStyle) == types.ClassType
True
--
http://mail.python.org/mailman/listinfo/python-list
Ahh much better. Thanks.
--
http://mail.python.org/mailman/listinfo/python-list
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 :)
10 matches
Mail list logo