[Python-Dev] Decorators with arguments are curries!
Phillip J. Eby
pje at telecommunity.com
Sat Aug 7 17:59:04 CEST 2004
At 10:36 PM 8/7/04 +1000, Andrew Durdin wrote:
>The first assignment to a is binding a reference to a function; the
>second is calling the function. This is a very significant difference
>in python, and I'm concerned that all the current proposed decorator
>syntaxes[*] are liable to cause confusion on this point. For example:
>
>def foo_decorator(func):
> print "no params to this"
> return func
>
>def bar_decorator(func, param):
> print param
> return func
>
>@foo_decorator
>@bar_decorator("one param here")
>def decorated_func():
> pass
>
>Here the first decorator statement is bare, while the second one
>includes parentheses and an argument; the first one looks like a
>function reference, while the second looks like a function call.
Your example will fail, saying that bar_decorator is being called without
enough arguments.
Decorator syntax does *not* provide currying. You have to write something
like this:
def bar_decorator(param):
def decorate(func):
print param
return func
return decorate
in order for your example to work.
More information about the Python-Dev
mailing list