[Python-Dev] @decoration of classes

Phillip J. Eby pje at telecommunity.com
Thu Mar 31 04:21:41 CEST 2005


At 10:05 AM 3/31/05 +1000, Nick Coghlan wrote:
>PJE's example of moving the decoration near the top of the class 
>definition without allowing class decoration contains an important caveat: 
>it requires that the decorators be written to support doing that. Allowing 
>class decoration means any appropriate decorators can be used, unmodified, 
>to affect classes as well as functions.

Yeah, but that can be trivially worked around using a 'decorate' function, 
e.g.:

from protocols.advice import addClassAdvisor

def decorate(*decorators):
     decorators = list(decorators)[::-1]
     def callback(cls):
         for dec in decorators:
             cls = cls(dec)
         return cls
     addClassAdvisor(callback)


class SomeClass:
     decorate(dec1,dec2,...)




More information about the Python-Dev mailing list