A simple decorator that does nothing can be done like follows:
def decorator(func):
return func # don't need to call!
@decorator
def print_word(word='python'):
print (word)
And that will run fine:
>>> print_word()
python
But then if I wrap that in an inner function, it is called liked so:
def decorator(func):
def _func(*args, **kwargs):
print ("Args: %s | Kwargs: %s" % (args, kwargs))
return func(*args, **kwargs) # <== Here I need to "call" it with ()
return _func
@decorator
def print_word(word='python'):
print (word)
>>> print_word('ok')
Args: ('ok',) | Kwargs: {}
ok
Why is it different in those two approaches? How does calling _func
"bind it" to _func(*args, **kwargs)
?