Named keywords in decorators?

Posted by wheaties on Stack Overflow See other posts from Stack Overflow or by wheaties
Published on 2010-06-13T14:36:28Z Indexed on 2010/06/13 14:42 UTC
Read the original article Hit count: 231

I've been playing around in depth with attempting to write my own version of a memoizing decorator before I go looking at other people's code. It's more of an exercise in fun, honestly. However, in the course of playing around I've found I can't do something I want with decorators.

def addValue( func, val ):
    def add( x ):
        return func( x ) + val
    return add

@addValue( val=4 )
def computeSomething( x ):
    #function gets defined

If I want to do that I have to do this:

def addTwo( func ):
    return addValue( func, 2 )

@addTwo
def computeSomething( x ):
    #function gets defined

Why can't I use keyword arguments with decorators in this manner? What am I doing wrong and can you show me how I should be doing it?

© Stack Overflow or respective owner

Related posts about python

Related posts about decorator