python装饰器的wraps作用

不加:

from functools import wraps
def my_decorator(func):
    def wper(*args, **kwargs):
        '''decorator'''
        print('Calling decorated function...')
        return func(*args, **kwargs)
    return wper

@my_decorator
def example():
    """Docstring"""
    print('Called example function')


print(example.__name__, example.__doc__)#wper decorator

加:

from functools import wraps
def my_decorator(func):
    @wraps(func)
    def wper(*args, **kwargs):
        '''decorator'''
        print('Calling decorated function...')
        return func(*args, **kwargs)
    return wper
 
@my_decorator
def example():
    """Docstring""" 
    print('Called example function')


print(example.__name__, example.__doc__)#example Docstring

装饰器修复技术

posted @ 2019-01-26 21:43  扫驴  阅读(415)  评论(1编辑  收藏  举报