python学习之装饰器的wraps作用
未加@wraps
# def tag(name):# def decorator(func):# def wrapper(text):# value=func(text)# return "<{name}>{value}</{name}>".format(name=name,value=value)# return wrapper# return decorator## @tag("p")# def my_upper(text):# value=text.upper()# return value# print (my_upper("hello"))# print (my_upper.__name__)输出:wrapper
加上@wraps
def tag(name): def decorator(func): @wraps(func) def wrapper(text): value=func(text) return "<{name}>{value}</{name}>".format(name=name,value=value) return wrapper return decorator@tag("p")def my_upper(text): value=text.upper() return valueprint (my_upper("hello"))print (my_upper.__name__)输入:my_upper二者之间的区别在于加上@wraps 原函数func被decorator(装饰器)作用后,函数性质不变
相关推荐
FlySky 2020-10-16
FlySky 2020-09-29
bizercsdn 2020-09-17
python0 2020-08-16
chenzulong 2020-08-16
LULUBAO 2020-07-08
一叶不知秋 2020-06-28
yogoma 2020-06-14
周小董 2020-06-10
hongxiangping 2020-06-09
xmwang0 2020-06-08
JJandYY 2020-05-31
Andrewjdw 2020-05-27
wklken的笔记 2020-05-27
zhuquan0 2020-05-26
chongtianfeiyu 2020-05-20
cas的无名 2020-05-19
qianjq 2020-05-10