[Python-Dev] method decorators (PEP 318)

Alex Martelli aleaxit at yahoo.com
Sat Mar 27 03:09:21 EST 2004


On 2004 Mar 27, at 06:27, Barry Warsaw wrote:
    ...
> attributes, but decorators can also do the trick in a nasty way:
>
> def foobar [
>     lambda f: f.author = 'Guido van Rossum',
>     lambda f: f.deprecated = True,
>     classmethod] (self, arg):
>     # Now what?

Not necessarily all that nasty:

def foobar [ with_attributes(
         author="Guido van Rossum",
         deprecated=True),
     classmethod] (cls, args):
         pass

with a built-in 'with_attributes' equivalent to:

def with_attributes(f, **kwds):
     for k, v in kwds.iteritems():
         setattr(f, k, v)
     return f


Alex




More information about the Python-Dev mailing list