Using metaclasses to inherit class variables
Steven Bethard
steven.bethard at gmail.com
Mon May 22 15:37:29 EDT 2006
telesphore4 at gmail.com wrote:
> Oops! This isn't working. As the sequence I'm trying for is....
>>>> def set_classvars(**kwargs):
> ... def __metaclass__(name, bases, classdict):
> ... for name, value in kwargs.iteritems():
> ... if name not in classdict:
> ... classdict[name] = value
> ... return type(name, bases, classdict)
> ... return __metaclass__
> ...
>>>> class C(object):
> ... __metaclass__ = set_classvars(name='foo', desc='bar', list=[])
> ... name = 'not foo'
> ...
>>>> C.name, C.desc, C.list
> ('not foo', 'bar', [])
>>>> class D(C):
> ... pass #<<<<<< Use Super's metaclass
> ...
>>>> D.name, D.desc, D.list, D.list is C.list
> ('not foo', 'bar', [], True)
What should the "right" answer be here? Maybe
('foo', 'bar', [], False)
or
('not foo', 'bar', [], False)
or something else?
STeVe
More information about the Python-list
mailing list