Multi-argument decorators in 2.6

Posted by wheaties on Stack Overflow See other posts from Stack Overflow or by wheaties
Published on 2010-06-11T14:11:09Z Indexed on 2010/06/11 14:12 UTC
Read the original article Hit count: 238

Filed under:
|

Generally don't do OO-programming in Python. This project requires it and am running into a bit of trouble. Here's my scratch code for attempting to figure out where it went wrong:

class trial(object):
    def output( func, x ):
        def ya( self, y ):
            return func( self, x ) + y
        return ya
    def f1( func ):
        return output( func, 1 )
    @f1
    def sum1( self, x ):
        return x

which doesn't compile. I've attempted to add the @staticmethod tag to the "output" and "f1" functions but to no avail. Normally I'd do this

def output( func, x ):
    def ya( y ):
        return func( x ) + y
    return ya

def f1( func ):
    return output( func, 1 )

@f1
def sum1( x ):
    return x

which does work. So how do I get this going in a class?

© Stack Overflow or respective owner

Related posts about python

Related posts about decorator