def warp(callback):
def f(*args, **kwargs):
print(' Before you call ')
callback(*args, **kwargs)
print(' After call ')
return f
@warp
def hello(name: str = ' Not set '):
print(f'hello {
name}')
if __name__ == '__main__':
hello('yimt')
Output
Before you call
hello yimt
After call
def warp(name: str):
def f1(callback):
def f2(*args, **kwargs):
kwargs['name'] = name
print(' Before you call ')
callback(*args, **kwargs)
print(' After call ')
return f2
return f1
@warp('yimt')
def hello(name: str = ' Not set '):
print(f'hello {
name}')
if __name__ == '__main__':
hello()
Output
Before you call
hello yimt
After call
def wrap(callback):
def f(*args, **kwargs):
print(' Before you call ')
r = callback(*args, **kwargs)
print(' After call ')
return r
return f
class A:
@wrap
def hello(self, name: str = ' Not set '):
print(f'hello {
name}')
a = A()
a.hello('yimt')
Output
Before you call
hello yimt
After call
def wrap(name: str):
def f1(callback):
def f2(*args, **kwargs):
kwargs['name'] = name
print(' Before you call ')
r = callback(*args, **kwargs)
print(' After call ')
return r
return f2
return f1
class A:
@wrap('yimt')
def hello(self, name: str = ' Not set '):
print(f'hello {
name}')
a = A()
a.hello()
Output
Before you call
hello yimt
After call
def warp_a(callback):
def f(*args, **kwargs):
print('a Before you call ')
callback(*args, **kwargs)
print('a After call ')
return f
def warp_b(callback):
def f(*args, **kwargs):
print('b Before you call ')
callback(*args, **kwargs)
print('b After call ')
return f
@warp_a
@warp_b
def hello(name: str = ' Not set '):
print(f'hello {
name}')
if __name__ == '__main__':
hello('yimt')
Output
a Before you call
b Before you call
hello yimt
b After call
a After call