As for the example in your first post:
var = 710
variable_name = [k fork, v inlocals().items()ifv == 710][0]
print("Your variable name is "+ variable_name)
it does "work", but it doesn't make much sense with Python's
semantics. You could have two identifiers bound to the same object;
which one you got hold of would be essentially random.
Yes, if `==` was replaced by `is`. Currently it is even more random as
it would return the first one which evaluates __eq__() positively.
I'm not sure what you're saying. In:
var1 = 710
var2 = 710
variable_names = [k for k, v in locals().items() if v is 710]
Depending on the Python implementation, variable_names may be ['var1',
'var2'] or it may be empty (depending on whether 710 is interned). It
could also in theory contain one of 'var1', 'var2' but not the other,
though I would be surprised if that happened in practice. The behaviour
is not guaranteed and should not be relied on.
Rob Cliffe
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/AI4OQP4SNIZORCI4JFJ5CV6QUVKIGYIB/
Code of Conduct: http://python.org/psf/codeofconduct/