Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ============== 方案1 ======================
- def non_decorator(func):
- run_something_before()
- result = func()
- run_something_after()
- return result
- def func1():
- print(123)
- def func2():
- print(456)
- # 我需要在func1和func2执行前后都做一些处理
- # 所以用定义的“非装饰器函数”来包装一下,变成这样执行
- non_decorator(func1)
- non_decorator(func2)
- # ================ 方案2 ======================
- def real_decorator(func):
- def wrapper():
- run_something_before()
- result = func()
- run_something_after()
- return result
- return wrapper
- @real_decorator
- def func1():
- print(123)
- @real_decorator
- def func2():
- print(456)
- # 执行
- func1()
- func2()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement