我得到“TypeError:decorator_factory()正好接受2个参数(给定1个)”

时间:2016-10-30 12:00:07

标签: python python-2.7 python-decorators

为什么我在此处运行调用时遇到上述异常。我觉得我错过了一些非常明显的东西......

def decorator_factory(arg1, arg2):
    def simple_decorator(f):
        def wrapper():
              print arg1
              f()
              print arg2
    return wrapper
return decorator_factory

@decorator_factory("what the heck", "what the heck2")
def hello():
print "Hello World"

hello()

1 个答案:

答案 0 :(得分:2)

必须是return simple_decorator而不是return decorator_factory

def decorator_factory(arg1, arg2):
    def simple_decorator(f):
        def wrapper():
              print arg1
              f()
              print arg2
        return wrapper
    return simple_decorator # <--- HERE 

@decorator_factory("what the heck", "what the heck2")
def hello():
    print "Hello World"

hello()
相关问题