With the help of @shadowRanger, I figured there's 2 ways to write the wrapper.
First way:
def offset_b(n, func):
def inner_wrapper(*args):
new_arg = (args[0], args[1]-n)
return func(*new_arg)
return inner_wrapper
print(offset_b(1, add)(1,2))
Because I don't HAVE to use @offset_b(0.5)
before function definition, the offset_b(n, func)
actually can take func
. It can save one layer of wrap.
In terms of the print function, offset_b(0.5, add)
gives the modified add function, then we can pass in (1,2)
2nd way (same as ShadowRanger shared):
def offset_b(n)
def outer_wrapper(func):
def inner_wrapper(*args)
return func(*args)
return inner_wrapper
return outer_wrapper
if we call offset_b(0.5)
, what we get is actually wrapper function.
Since how we use wrapper is func = wrapper(func)
what we need to do is to get the new function by
offset_b(0.5)(add)
then we give (1,2)
to that function, which gives us offset_b(0.5)(add)(1,2)