@dec_sub
def sub(a, b):
c= a-b
return c
def dec_sub(func):
def wrapper(a, b):
if a<b:
a, b = b, a
return func(a, b)
return wrapper
print(sub(48, 9)) # first digit is bigger than the second one yields positive return
print(sub(1, 8)) # second digit is bigger than the first one also yields positive return
#Output : 39
7
In the above code, how to use the function 'sub' in a usual way without the influence of the decorator?
CodePudding user response:
You don't need to use decorator syntax to create a decorated function.
def sub(a, b):
c = a - b
return c
wrapped_sub = dec_sub(sub)
Or, you can use func_tools.wraps
to create an object that exposes a reference to the original function.
from functools import wraps
def dec_sub(func):
@wraps(func)
def wrapper(a, b):
if a<b:
a, b = b, a
return func(a, b)
return wrapper
@dec_sub
def sub(a, b):
c = a - b
return c
sub(48, 9) # the decorated function
sub.__wrapped__(48, 9) # the original function