Python3 study notes ---- decorator

The role of decorator

Extend the original function without modifying the original function and call mode

Create a decorator

def decorator(func):
      def inner(*args,**kwargs):
      ''' 执行函数之前要做的 '''
          res = func(*args,**kwargs)    
      ''' 执行函数之后要做的 '''
          return res
      return inner
''' 在使用装饰器后,在装饰器里访问函数的元数据时,看到的是装饰器函数的元数据;functools.wraps 则可以将原函数对象的指定属性复制给包装函数对象'''
from functools import wraps
def decorator(func):
      @warps(func) 
      def wrapper(*args,**kwargs):
      ''' 执行函数之前要做的 '''
          res = func(*args,**kwargs)  
       ''' 执行函数之后要做的 '''  
          return res
      return wrapper

Guess you like

Origin www.cnblogs.com/yuky/p/10973926.html