[Python-Dev] method decorators (PEP 318) (original) (raw)
Skip Montanaro skip at pobox.com
Sat Mar 27 13:11:40 EST 2004
- Previous message: [Python-Dev] method decorators (PEP 318)
- Next message: [Python-Dev] method decorators (PEP 318)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Barry> attributes, but decorators can also do the trick in a nasty way:
Barry>
Barry> def foobar [
Barry> lambda f: f.author = 'Guido van Rossum',
Barry> lambda f: f.deprecated = True,
Barry> classmethod] (self, arg):
Barry> # Now what?
Alex> Not necessarily all that nasty:
Alex> def foobar [ with_attributes(
Alex> author="Guido van Rossum",
Alex> deprecated=True),
Alex> classmethod] (cls, args):
Alex> pass
Both cases demonstrate why I don't like the decorator-before-args option. Where are the args? Like I said in an earlier post most of the time I look at a function def wanting a quick reminder how to call it. If the argument list doesn't at least start on the same line as the def I think that will be tougher.
Alex> with a built-in 'with_attributes' equivalent to:
Alex> def with_attributes(f, **kwds):
Alex> for k, v in kwds.iteritems():
Alex> setattr(f, k, v)
Alex> return f
Not quite. It has to be written as a function which returns another function, e.g.:
def attrs(**kwds):
def decorate(f):
f.__dict__.update(kwds)
return f
return decorate
Skip
- Previous message: [Python-Dev] method decorators (PEP 318)
- Next message: [Python-Dev] method decorators (PEP 318)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]