I'm trying to conquer one of the final basic python features that I've avoided using since I started: decorators. I'm not grocking it like i did with list-comps, and I do not understand how an inner function within a decorator declaration works.
Here's an example of what I mean. Given this chunk-o-code:
def outer(func):
def inner(*args, **kwargs):
print('Hi my name is ')
return func(*args, **kwargs)
return inner
@outer
def decorated(name):
print(name)
decorated('Bob')
I understand that this this will print
Hi my name is
Bob
but what I don't understand is how inner obtains any *args or **kwargs from decorated()
My understanding is that
@outer
def decorated(name):
print(name)
decorated("Bob")
is equivalent to outer(decorated("Bob")). If this is the case, how would inner() be able to access the name argument? Syntax issues aside, I'd expect the declaration for inner to look like def inner(func.args, func.kwargs):
What's going on here? What am I misunderstanding?