I am trying to wrap my head around decorators. So I tried passing arguments to a decorator and processing them inside the decorator function. I just pass a list to the decorator and want my list to be processed inside the decorator function after calling the original function. Here's my code
def decorator_function(original_function):
def new_function(*args, **kwargs):
print("Your list shall be processed now.")
print args
#Do something with the passed list here.Say, call a function Process(list,string)
original_function(*args, **kwargs)
return new_function
@decorator_function([1,2,3])
def hello(name = None):
if name == None:
print "You are nameless?"
else:
print "hello there,",name
hello("Mellow")
I get this error
Your list shall be processed now.
(<function hello at 0x7f9164655758>,)
Traceback (most recent call last):
File "anno_game.py", line 14, in <module>
def hello(name = None):
File "anno_game.py", line 8, in new_function
original_function(*args, **kwargs)
TypeError: 'list' object is not callable
Can anyone please tell me what have I messed up here and point me in the right direction?
When you do
@decorator_function([1,2,3])
decorator_function
is called with[1,2,3]
passed to it asoriginal_function
argument, which you are trying to calloriginal_function(*args, **kwargs)
.To have the decorator to receive a list, you need to make another wrapper: