[Baypiggies] JJ's decorators

Aahz aahz at pythoncraft.com
Sun Mar 8 16:08:58 CET 2009


On Sun, Mar 08, 2009, Smith1, Robert E wrote:
>Aahz:
>> 
>> It doesn't.  The problem is that there are really two different kinds of
>> decorators, those without arguments and those that take an argument.
>> Unfortunately, the second kind is more complicated,
> 
> Thanks for the reply. Now that I think about it, I do seem to recall
> Charles Merriam saying that "you get called twice" in the case of
> decorators with arguments. So the first return - "return {"pre":
> pre_logged, "post": post_logged}[when]" - must be to give Python the
> function to call the second time during setup. To my untrained eye it
> looks like the "decorator with arguments" scenario could have been
> handled with a single call that included the decorated function as an
> argument in addition to the arguments for the decorator, but I must be
> missing something.

While writing that post, I looked at PEP 318 to figure out why it was
done, and the most I was able to find was that

    @deco('bar')
    def foo(): pass

was equivalent to

    def foo(): pass
    foo = deco('bar')(foo)

without any justification for why that was preferred over

    foo = deco(foo, 'bar')

I'm assuming there was some prior art or a preference for simplifying the
call signature for deco(), but unless someone asks on python-dev (or can
search the python-dev archives or is reading baypiggies and decides to
answer), I don't think we'll get an authoritative answer.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"All problems in computer science can be solved by another level of     
indirection."  --Butler Lampson


More information about the Baypiggies mailing list