We know that in python you can get the name of a defined function: eg.
def f(x):
return x
print(f"we've just defined function {f.__name__}")
Let's say i create a variable:
x = 4
Now at some point i would like to do:
print(f"you have defined a variable named {x.__name__}")
But the syntax above gives an error because x
doesn't have such an attribute.
Is there a way to get the variable's name from its value, just as we can do with the value of a defined function ?