0

I'd like to be able to use a wrapper on a class method:

def wrapper(f):
def inner(*args,**kwargs):
    print 'inner '+f.__name__
    x = f(*args,**kwargs)
    return x
return inner

class A:
    @wrapper
    def f1(x=55):
       return x

print A().f1()

This returns <main.A instance at 0x05FF7530>

How can I return the result of the wrapped function A.f1()?

1
  • 2
    please fix the indentation Commented Jun 8, 2014 at 15:09

2 Answers 2

2

You have forgotten the self-argument:

class A:
    @wrapper
    def f1(self, x=55):
       return x
Sign up to request clarification or add additional context in comments.

Comments

0

If you truly want to call it as stated in the question, make it a classmethod:

class A:
    @classmethod
    @wrapper
    def f1(self, x=55):
       return x
>>> A.f1()
inner f1
55
>>>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.