Python 2.4 Decorators
To date, I have disagreed (though not publically) with the claims that
Python keeps getting more and more like lisp. But to me, the new
(Python 2.4) decorators are two very large steps in that direction.
First: is there any difference in usage/intent between decorators
and the old property lists that ancient LISPs like elisp used to use?
Second: The apparently primary decorator usage pattern
screams out for a multi-line lambda. Hans Nowak wrote
a very clear description of what decorators are, and this
pattern shows up in the three main decorator functions listed
there (attrs, wrapwith, and addto). Each one defines a decorate
or decorator function and then returns it.
Wouldn't they all be better as lambdas?
(Here, I've used a fake keyword "lreturn" to indicate my
uncertainty about whether multi-line lambdas would read
better with a return, or without, or with a different keyword
as shown here.)
def attrs(**kwds):
return lambda f:
for k in kwds:
setattr(f, k, kwds[k])
lreturn f
def wrapwith(obj):
return lambda f:
return lambda *args, **kwargs:
print "##", obj
lreturn f(*args, **kwargs)
def addto(instance):
return lambda f:
import new
f = new.instancemethod(f, instance, instance.__class__)
setattr(instance, f.func_name, f)
lreturn f
10:31:16 AM