I would like to use decorators to convert numbers in string format to int/float value, here is how I am trying to do it
def str_to_int(func):
"""
This wrapper converts string value into integer
"""
def wrapper(*args, **kwargs):
for arg in args:
arg = int(arg)
return func(*args, **kwargs)
return wrapper
@str_to_int
def number_as_string(a, b, c):
return a,b,c
print (number_as_string('1', '2', '3'))
Output
('1', '2', '3')
However, I wanted something like below
(1, 2, 3)
The above output I could generate with the below code
def str_to_int(func):
"""
This wrapper converts string value into integer
"""
def wrapper(x, y, z):
return int(x), int(y), int(z)
return wrapper
@str_to_int
def number_as_string(a, b, c):
return a,b,c
print (number_as_string('1', '2', '3'))
But the above code defeats the purpose of using a decorator at first place, since I would like to convert the string values irrespective of the arguments the original function has.
Could any one suggest what is wrong with first program and how to resolve it.