您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

装饰器错误:NoneType对象不可调用

装饰器错误:NoneType对象不可调用

您应该返回包装函数本身, 而不是其结果

def tsfunc(func):
    def wrappedFunc():
        print '%s() called' % func.__name__
        return func()
    return wrappedFunc   # Do not call the function, return a reference instead

装饰器将装饰的物品替换为装饰器的返回值:

@tsfunc
def foo():
    # ....

等效于:

def foo():
    # ....
foo = tsfunc(foo)

扩展为(在您的代码中):

foo = wrappedFunc()

因此,您是foo使用wrappedFunc()调用结果而不是wrappedFunc自身替换函数

dotnet 2022/1/1 18:29:53 有343人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶