Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanxiao/a5c08238a5600b48f1e0f08ae68cab83 to your computer and use it in GitHub Desktop.
Save hanxiao/a5c08238a5600b48f1e0f08ae68cab83 to your computer and use it in GitHub Desktop.
python in class decorator with parameter
class Baaar:
def __init__(self):
self.z = 10
def wrapper(v:int = 0):
def f2(f):
def f2_v(self, *args, **kwargs):
print('z=%d' % self.z)
return f(self, *args, **kwargs) + v
return f2_v
return f2
@wrapper(4)
def foo(self, x:int):
return 3+x
@wrapper(4)
def foo1(self, x:int):
return 3+x+self.z
@wrapper(4)
def foo2(self, x:int, y: int):
return 3+x*y + self.z
a = Baaar()
print(a.foo(1))
print(a.foo1(1))
print(a.foo2(1,6))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment