带参数的函数装饰器
-----------------------------带参数的函数装饰器---------------------------
- 基本格式
def x(counter):
def wrapper(func):
def inner(*args,**kwargs): #执行原函数并获取返回值
data = func(*args,**kwargs)
return data
return inner
return wrapper
@x(9)
def index():
pass
-
练习题
#输入参数几就执行几次 def x(counter): print('x函数') def wrapper(func): print('wrapper函数') def inner(*args,**kwargs): #执行原函数并获取返回值 v = [] for i in range(counter): data = func(*args,**kwargs) v.append(data) return v return inner return wrapper @x(9) def index(): return 1 result = index() print(result) #爱的魔力转圈圈,我好懵逼
ads
记录学习的点点滴滴