I have written the following function:
def get_id(arg_a: int, arg_b: int, arg_c: int, arg_d: int) -> str:
'''Construct some example id for stackoverflow.'''
if isinstance(arg_a, float):
arg_a = int(arg_a)
if isinstance(arg_b, float):
arg_b = int(arg_b)
if isinstance(arg_c, float):
arg_c = int(arg_c)
if isinstance(arg_d, float):
arg_d = int(arg_d)
return f'{arg_a}|{arg_b}|{arg_c}|{arg_d}'
When I started to refactor my program, I found this function to be written not in as much pythonic way as I would like, so I started to think, how to rewrite it. I found out, I could use exec() in combination with get_id.__code__.co_varnames, which will provide a tuple with function arguments to iterate over:
def get_id(arg_a: int, arg_b: int, arg_c: int, arg_d: int) -> str:
'''Construct some example id for stackoverflow.'''
for arg in get_id.__code__.co_varnames:
exec(f'{arg} = int({arg}) if isinstance({arg}, float) else {arg}')
return f'{arg_a}|{arg_b}|{arg_c}|{arg_d}'
But after that, I found out that exec() function is not suggested to be used in production programs from many reasons, which I understand. But I do not know, what to use instead of exec(). Please, could you help me, how to write my function in more general way and without exec() function?
Thanks for your help.