[Python-Dev] order of decorator application?
Bob Ippolito
bob at redivi.com
Tue Mar 23 14:15:14 EST 2004
On Mar 23, 2004, at 12:48 PM, Jewett, Jim J wrote:
> Skip:
>
>> Is there a concensus on the order of application for decorators?
>
> I think people will accept either; the question is what counts as
> "first".
>
> In
> name = a(b(c(name)))
>
> Is a first because it appears (and is resolved) first, or is c
> first because it is applied first?
>
> This also interacts with whether the decorators are before or
> after the function name, and with whether we're modelling
>
> name = a(name)
> name = b(name)
> name = c(name)
>
> or
>
> name = a(b(c(name)))
I think that the majority opinion is that they should be evaluated left
to right, which sounds sensible to me because that's how everything
else works in Python until you start using lots of parentheses. I
would probably "accept" either, but I would be very disappointed if it
wasn't left-to-right.
Either way, if you had a real strong opinion about one direction over
the other, you could write a function that takes a list of functions
and applies them in the order you would like them applied, eliminating
any ambiguity. The most obvious function to do this would probably
look something like:
# warning: untested, I don't care if it actually works, it's close
enough
def left_to_right(*args):
def left_to_right(fn):
return reduce(lambda a,b:b(a), args, fn)
return left_to_right
def decorated()[left_to_right(a, b, c, d)]:
pass
-bob
More information about the Python-Dev
mailing list