slopeofhope slopeofhope - 1 month ago 9
Python Question

I am getting "TypeError: decorator_factory() takes exactly 2 arguments (1 given)"

Why am I getting the above exception from running run the call here. I feel like I am missing something very very obvious..

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()

Answer

It has to be return simple_decorator instead of 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()