Concept 3 in python-decorator

Decorator
  • During the code is running, the way to dynamically increase the function
  • decoratorThe essence of is a higher order function
  • decoratorAccepts a function as a parameter and returns a function

Examples of decorators:

import functools
def log(text):
	def decorator(func):
		@functools.warps(func)
		def wap(*args, **kw):
			print (text)
			func(*args, **kw)
		return wap
	return decorator

@log("this is a test")
def now()
	print (datetime.datetime.now())

Which @functools.warps(func)is to __name__process the properties of the function

Guess you like

Origin blog.csdn.net/Free_time_/article/details/107597148