1. 程式人生 > >python裝飾器的wraps作用

python裝飾器的wraps作用

rap docstring cst class too called ted rom style

不加:

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

裝飾器修復技術

python裝飾器的wraps作用