其他分享
首页 > 其他分享> > functools

functools

作者:互联网

  

import functools
def logger(fn):
    # @functools.wraps(fn)

    def wrapper(*args,**kwargs):
        'this is wrapper'
        print('before call fn')
        kin=fn(*args,**kwargs)
        print('after call fn')
        return kin
    # functools.update_wrapper(wrapper,fn)
    functools.wraps(fn)(wrapper)
    print('{}\t{}'.format(wrapper,fn))
    #return wrapper
   return functools.update_wrapper(wrapper,fn)
        
@logger def kip(x,y): ''' this is kip function return int x int y int :param x: :param y: :return: int ''' ret=x+y return ret print(kip.__name__,kip.__doc__,kip.__qualname__,sep='\n') print('@'*50) print(kip.__wrapped__)

 

functools.update_wrapper(wrapper,wrapped,assigned=WRAPPER_ASSIGNMENTS,updated=WRAPPER_UPDATES)

类似copy_properties功能

wrapper包装函数,wrapped被包装函数

元组WRAPPER_ASSIGNMENTS中是要被覆盖的属性

元组WRAPPER_UPDATES中是要被更新的属性,__dict__ 属性字典

增加__wrapped__ 属性,保留wrapped函数

        

标签:__,functools,kip,wrapper,print,fn
来源: https://www.cnblogs.com/dissipate/p/13619653.html