Is there any way in Python to build a dict from variable name/value to key/value without assigning explicitly?
def my_func(var1, var2):
my_dict = dict(var1 , var2)
print(my_dict)
my_func("x", "y")
Prints:
{"var1": "x", "var2": "y"}
Edited in order to make it less artificial. The idea is to avoid dict(var1=var1)
var1andvar2?my_dict = {'var1': var1, 'var2': var2}. Unless I'm missing something...?dict(var1=var1, var2=var2)'var1'and'var2'explicitly as string constants. Where you writevar1, this means: the object to which the namevar1refers. That object itself contains no concept of any variable name to which it might be assigned, and indeed the same object can be assigned to multiple variables.