编写装饰器:
函数定义装饰器
def p_decorator(func):
def wrapper(*args,**kwargs):
return '<p>' + func(*args,**kwargs) +'</p>'
return wrapper
@p_decorator
def get_text():
return '欢迎学习'
if __name__ =='__main__':
print(get_text())
//或:
def p_decorator(func):
def wrapper(*args,**kwargs):
return '<p>' + func(*args,**kwargs) +'</p>'
return wrapper
def get_text():
return '欢迎学习'
if __name__ =='__main__':
html = p_decorator(get_text)
print(html())
//修饰带参数的情况
def p_decorator(func):
def wrapper(*args,**kwargs):
return '<p>' + func(*args,**kwargs) +'</p>'
return wrapper
@p_decorator
def get_upper_text(text):
return text.upper()
if __name__ =='__main__':
html = p_decorator(get_text)
print(html())