Python 带有参数的装饰器
原创
©著作权归作者所有:来自51CTO博客作者wx5861be15b7630的原创作品,请联系作者获取转载授权,否则将追究法律责任
1. 装饰器带有参数(比如:retry)
import time
def call_out(timeout=0):
def check_login(func):
def wrapper(*args, **kwargs):
print("-------1--------")
time.sleep(timeout)
ret = func(*args, **kwargs)
print("--------2-----------")
return ret
return wrapper
return check_login
@call_out(2)
def print_hello():
print("hello world")
return "ok"
print(print_hello())
2. 输出结果
C:\Users\HuJun\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/HuJun/PycharmProjects/pythonProject/daily_tesy/装饰器带有参数.py
-------1--------
hello world
--------2-----------
ok
Process finished with exit code 0