Hi,
I have two near identical pieces of code, from practice learning decorators. See below.
My question is, how does decor_with_arg knows that foo is silly_foo? foo is never assigned anything in code. Also, why does 'inside foo' gets printed twice, but 'inside foo 2' gets printed once if both function are called once?
Thanks for the help.
I have two near identical pieces of code, from practice learning decorators. See below.
My question is, how does decor_with_arg knows that foo is silly_foo? foo is never assigned anything in code. Also, why does 'inside foo' gets printed twice, but 'inside foo 2' gets printed once if both function are called once?
Thanks for the help.
# example 1 def decor_with_arg(argument): def do_something(foo): # <- how does this know what foo is if its never declared? print("before foo inside decorator") foo() print("after foo inside decorator") return foo return do_something @decor_with_arg(None) def silly_foo(): print('inside foo') silly_foo() # example 2 def decor_2(foo): def do_something(): print("before foo inside decorator 2") foo() print("after foo inside decorator 2") return foo return do_something @decor_2 def silly_foo2(): print('inside foo 2') silly_foo2()