[Python-Dev] Class decorators

Guido van Rossum guido at python.org
Tue Mar 28 06:02:20 CEST 2006


On 3/27/06, Phillip J. Eby <pje at telecommunity.com> wrote:
> PyProtocols and the zope.interface package both support inline class
> decorators called "class advisors".  They don't require any special syntax,
> and aren't much more complex than regular decorators.  By defining an
> advisor like this:
>
>     from protocols.advice import addClassAdvisor
>
>     def some_advisor(whatever_args):
>         def callback(cls):
>             print "I can modify",cls,"or replace it"
>             return cls
>         addClassAdvisor(callback)
>
> you can then use it in a class body like so:
>
>      class SomeClass:
>          some_advisor("something")
>
> And the return value from 'callback' will replace SomeClass, just like a
> decorator replaces the function it's called on.
>
> The implementation should work with any Python version from 2.2 up.  I'm
> not sure if it would work with IronPython.  But if it doesn't, that would
> be a good indication of feature(s) that IronPython is missing.  ;)

Just curious (and lazy): what magic is the implementation using that
makes this work without a custom metaclass?

--
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list