Is it possible to write a function f that takes an arbitrary tuple of mixed data:
T = 1.0
N = 20
L = 10
args = (T,N,L)
f(*args) # or maybe f(T,N,L)?
and returns as output:
{'T':1.0, 'N':20, 'L':10}
There is a related question using local, but I seem to lose the names once they are passed to the function. Is there a way to prevent this? I’m guessing that the variables are being passed by value and thus they are considered new objects.
Followup: Python: Using a dummy class to pass variable names?
No, this is not possible in general with
*args. You’ll have to use keyword arguments:The reason is that
*argsdoes not introduce names for the individual arguments; it only introduces the nameargsfor the whole list of them. The function has no insight into how what names, if any, the arguments have outside of it.When the number of arguments is fixed, you can do this with
locals:(or explicitly with
return {'T': T, 'N': N, 'L': L}.)