On 1/22/20 7:08 AM, Rhodri James wrote:
On 22/01/2020 12:03, Anders Hovmöller wrote:
He was pretty clear in saying he wanted the same thing as in C#. That no one in this thread seems to have looked that up is really not his fault in my opinion.

Oh, plenty of people have looked it up.  The problem is that it relies on part of the nature of C# (variables are lvalues) that simply isn't true in Python, which I don't think the OP realised.

Or to expand on that, in C# and related languages, variable names actually HOLD some value (which might be a special type of value called a pointer), and a given object will either be in a specific variable name, or off somewhere in the heap with no name. We might have a variable with a pointer value to another object, and there it can be useful to query the object through that pointer and ask what is the name of the variable you are in, as we might not know it at the site of usage.

In Python, variables don't hold values, but refer to them. A given value can easily be referred to by many names equally, and there is no such thing as a pointer (which gets you to a name) only multiple references to the same object.

Thus nameof(x) can ONLY be "x" in Python (or an error is x isn't something that is a name), as at best x is referring to some object, but that object doesn't have a special name to refer to it that is its holder.  Yes, one example where that linkage might be useful is it documents in the code that the string value is actually supposed to be the name of the variable, so a refactoring that changes the variable should change the string too, such a semantic link between a variable and its spelling is ill advised, and would only be used in a debugging environment with something like print("x", x) or some similar purpose function, and in that context, such prints should not normally be long lived or are part of long term logging in which case the stability of the label might actually be preferred to not break parsers for the log output.

--
Richard Damon
_______________________________________________
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/RSBOLYY7JEIMOMXVCB5MBKZFWMNUNCIR/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to