装饰器调用错误

![![![![]
加上装饰器之后,所有功能就开始从上到下运行,是装饰器写的有问题吗?还是我用得不对?

Jason990420
最佳答案

Decorator

def decorator(func):
    def new_function(*args, **kwargs):  # New function defined
        """ Actions before 'func' called """
        value = func(*args, **kwargs)   # Call `func` and keep the result
        """ Action after 'func' called """
        return value                    # Return the result
    return new_function                 # Return the new function

Last line in your decorator function, it should return the new function, but it execute the function without any arguments.

return inner()

Should be

return inner
2周前 评论
ephemeral (楼主) 2周前
讨论数量: 2
Jason990420

Decorator

def decorator(func):
    def new_function(*args, **kwargs):  # New function defined
        """ Actions before 'func' called """
        value = func(*args, **kwargs)   # Call `func` and keep the result
        """ Action after 'func' called """
        return value                    # Return the result
    return new_function                 # Return the new function

Last line in your decorator function, it should return the new function, but it execute the function without any arguments.

return inner()

Should be

return inner
2周前 评论
ephemeral (楼主) 2周前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!