On Tue, Jan 21, 2020, at 16:32, Andrew Barnert via Python-ideas wrote: > What would the semantics of nameof be in Python? Would it just be > lambda obj: obj.__name__? Or some fancy inspect-module style chain of > “try this, then that, then the other”? Or does it need to look at the > compiled source code context or something?
The C# version becomes a string constant and doesn't have to look at anything other than the expression itself. It is the literal name of the variable [or attribute] in the expression, irrespective of its value. The intent is for it to be a string literal that survives refactors, so you could do nameof(a_person.age) and that would compile down to "age" [without actually performing an attribute access. C# allows you to do this with instance members without making an instance, but it is unclear what the best way to do this in python would be], but if you used a refactoring tool to rename the age attribute, it would change this expression along with all of the other places it is used, to give it the new attribute name. There's no fancy run-time lvalue reflection stuff going on as you suggested in your other post, it is just the name from the expression, as a string constant. It is purely a compile time operation (which is why it has to be an operator, not a function) _______________________________________________ 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/7IZRVFY2QB5LFDYL4MWL7L6I7GRV5SFP/ Code of Conduct: http://python.org/psf/codeofconduct/