装饰器

functools.wraps

from functools import wraps # 装饰器修复器

# 保证被装饰器装饰后的函数还拥有原来的属性

def go(func):
	@wraps(func)
	def inner(*args, **kwargs):
		'''
		This is decorator inner function
		'''
		print('before')
		ret = func(*args, **kwargs)
		print('after')
		return ret
	return inner

@go
def say_hello(name):
	'''
	This is say_hello function
	'''
	print(f'hello {name}')

# 不加装饰器修复器 @wraps(func), 

# say_hello.__name__ 显示的是inner函数,不能显示say_hello的属性, __doc__等都是inner函数

say_hello('sunny')
print(say_hello.__name__)
print(say_hello.__doc__)
原文地址:https://www.cnblogs.com/relaxlee/p/12842461.html