GIGO: words unreadable aloud
Mishrogo Weedapeval
 

 

  Saturday 14 August 2004
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   comment/     


Click here to visit the Radio UserLand website. Click to see the XML version of this web page. © Copyright 2007 Doug Landauer .
Last update: 07/2/6; 12:41:35 .
Click here to send an email to the editor of this weblog.

August 2004
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Jul   Sep

Previous/Next