Getting the name of an assignment
Steven Bethard
steven.bethard at gmail.com
Sun Dec 24 00:25:46 EST 2006
Adam Atlas wrote:
> Isn't it a bit convoluted to use metaclasses?
Yep. It's a well known fact that putting "convoluted" and "metaclasses"
in the same sentence is repetitively redundant. ;-)
> someinstance.__class__.__name__ does the same thing.
No, not really::
>>> class C(object):
... def __init__(self, name):
... self.name = name
... @classmethod
... def from_class_block(cls, name, bases, blockdict):
... return cls(name)
...
>>> c = C('foo')
>>> c.name
'foo'
>>> type(c)
<class '__main__.C'>
>>> c.__class__.__name__
'C'
>>> class foo:
... __metaclass__ = C.from_class_block
...
>>> foo.name
'foo'
>>> type(foo)
<class '__main__.C'>
>>> foo.__class__.__name__
'C'
Note that the ``class foo`` statement is not creating a class. It's
creating an instance of ``C``. So it really is doing something pretty
different.
STeVe
More information about the Python-list
mailing list