Monday, December 27, 2010

metaclass abuse: conjoined classes

>>> class ConjoinedMeta(type):
...    def __new__(cls, name, bases, attrs):
...       for k,v in attrs.items():
...          setattr(ConjoinedMeta, k, v)
...       return ConjoinedMeta
...
>>> class A(object):
...    __metaclass__ = ConjoinedMeta
...    a = "a"
...
>>> A
<class '__main__.ConjoinedMeta'>
>>> class B(object):
...    __metaclass__ = ConjoinedMeta
...
>>> B.a
'a'
>>> B.b = 'b'
>>> A.b
'b'

No comments:

Post a Comment