On Jan 21, 2020, at 13:32, Andrew Barnert <abarn...@yahoo.com> wrote: > > On Jan 21, 2020, at 10:48, Johan Vergeer <johanverg...@gmail.com> wrote: >> >> def __repr__(self): >> return f"{nameof(Person)}({nameof(self.name)}: {self.name}, >> {nameof(self.age)}: {self.age})" > > 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?
Sorry; I didn’t notice the nameof(self.age) part there. That definitely does need to look at the source (or something else, like the bytecode), because whatever value is in self.age doesn’t know that it’s in self.age (especially since it’s probably in lots of other variables as well); all it knows is that it’s the int object with value 42. But it seems like nameof(self.age) always has to be just “age”, which you already have to type inside the nameof if you want it to be findable, so it’ll never be useful. Anywhere you could write `{nameof(self.age)}` you could just more simply write `age`. The big difference in C# is that variables are lvalues. In Python, self.age is just a reference to that int object 42 somewhere; in C#, this.age is a reference to the int-sized, int-typed location 8 bytes after the start of “this” (which can decay to the int value stored there on demand), and the runtime can use that to figure out that it’s the “age” attribute of this via reflection (or maybe the compiler can do that). This also means you can pass around a reference to this.age and mutate this through that reference (and presumably nameof works on that reference too), which doesn’t make any sense in Python. _______________________________________________ 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/AB5HVE4XJKZ57PR2RIYABVLMBUIEESIX/ Code of Conduct: http://python.org/psf/codeofconduct/