[Python-Dev] Decorators with arguments are curries! (original) (raw)

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 foodecorator(func): print "no params to this" return func def bardecorator(func, param): print param return func @foodecorator @bardecorator("one param here") def decoratedfunc(): 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