I agree. What I meant is that it is unreliable with `v is 710` and even more unreliable with `v == 710` as β==β is less strict than βisβ.
DG > On 12 Sep 2023, at 21:03, Rob Cliffe <rob.cli...@btinternet.com> wrote: > > >>> As for the example in your first post: >>> var = 710 >>> variable_name = [k for k, v in locals().items() if v == 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/JOCYEOKCQHEOLKF2LA2YMCFY7BL7XTKY/ Code of Conduct: http://python.org/psf/codeofconduct/