Friday, August 5, 2011

Decorate then Meta, or Meta then Decorate?

Which is applied second, a meta-class or a class decorator?

>>> def add_a(cls):
...    cls.a = "decorator!"
...    return cls
...
>>> class MetaA(type):
...    def __new__(cls, name, bases, attrs):
...       attrs["a"] = "metaclass!"
...       return type.__new__(cls, name, bases, attrs)
...
>>> @add_a
... class Test(object):
...    __metaclass__=MetaA
...
>>> Test.a
'decorator!'

No comments:

Post a Comment