装饰器是一种函数,它可以修改其他函数的功能。
-
- def decorate(func):
- def decorate_real(*args, **kwargs):
- print('decorate begin')
- func(*args, **kwargs)
- print('decorate end')
- return decorate_real
-
- @decorate
- def test(name, age):
- print('hello', name, age)
-
- test('123', 456)
-
-
- class Decor(object):
- def __call__(self, func):
- def decorate_real(*args, **kwargs):
- print('decorate2 begin')
- func(*args, **kwargs)
- print('decorate2 end')
- return decorate_real
-
- @Decor()
- def test2(name, age):
- print('hello2', name, age)
-
- test2('123', 456)