On 2004 Mar 27, at 19:11, Skip Montanaro wrote: > 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. As long as the decorators don't come between the def and the function name (that WOULD be far too early and impede looking for 'def foo' to easily locate the definition) I'll be OK, but, sure, I do agree that right before the colon is the best place for the decorators. I was just using the same hypothetical syntax as Barry was using in his own snippet. > 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 Yes, you're right, this is what the semantics must be (assuming that "setattr(f, n, v)" does absolutely nothing more than "f.__dict__ [n] = v", a hypothesis I wasn't ready to make -- I'm not sure it's currently guaranteed by the language reference manual -- but if I'm wrong and it IS so guaranteed and I just missed the spot thus guaranteeing, the bulk update is of course a speedier approach than the loop I had). Alex
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4