def pass_through1(func):
def inner1(*args):
test_logs.append("pt1")
return func(*args)
return inner1
def pass_through2(func):
def inner2(*args):
test_logs.append("pt2")
return func(*args)
return inner2
@pass_through1
@pass_through2
def increment(num):
return num+1
test_logs=[]
increment(10) //gives me the answer as 11
test_logs=['pt1','pt2'] //now test_log contains after the calling the increment function
Now my doubt is did the increment function gets executed twice? As we are passing it to the two decorators.
You aren't passing increment
to both decorators; you pass increment
to the decorator pass_through2
and this returns a function named inner2
which is passed to the other decorator pass_through1
. That decorator returns a function named inner1
which is finally bound to the name increment
.
So when you call increment
, it really calls inner1
, which then calls inner2
, which calls the original increment
function - once - and each of inner1
and inner2
do their logging once.