On Tue, Jan 21, 2020 at 02:25:56PM -0500, Ricky Teachey wrote: > Isn't the name of the class more reliably `type(ins).__qualname__`?
Depends on whether you want the plain old name of the class, or the qualified name of the class. > At any rate, I've actually wished for a shortcut to the name of an object's > class- of the name of the class object itself- many times in the past. Write a one-line helper function that returns type(obj).__name__ or __qualname__, whichever you prefer. > Mostly when writing reprs and exception messages. Not sure if that is in > line with OP's suggestions. No. As far as I can tell, Johan wants to capture the name of a name binding, not the name of a class. For example, given: x = 1 then you want nameof(x) to return 'int' (the name of the class) and Johan wants it to return 'x' (the name of the variable). But what should these things do? x = y = z = 1 nameof(1) nameof(z) nameof("Gumby") nameof(mysequence[0]) It isn't clear what precisely counts as the name of a variable. Johan's nameof operator looks like a function. But it can't be a function, for obvious reasons. Or at least, they're obvious to me, I don't know if Johan understands why a regular function can't easily/efficiently/unambiguously return the name of an object. Johan, it would be nice when making a proposal if you spelled out precisely what you expect it to do, as well what you don't expect it to do, especially if we don't know you well enough to predict if you are an expert or a novice. Otherwise we risk either talking down to you, or talking over your head. So for obvious reasons, nameof() cannot be a regular callable object. It has to be some sort of magic behind it so that if we write this: x = y = None nameof(x) nameof(y) the first call returns "x" and the second call returns "y", even though both objects are identically None. Fortunately we can already do this from Python today, using special variable quoting syntax. Instead of writing `nameof(x)`, just replace the function call with quote marks, and at compile time Python will compile in the name of the variable 'x' as if by magic. And best of all, quoting a variable name in this way is only two keypresses instead of ten. (It also works with double quotes too.) /deadpan Okay, I'm kidding, but only a little bit. Nevertheless, `nameof` is not an ordinary function, and it shouldn't look like an ordinary function. I don't buy Johan's argument: "This makes sure the developer cannot forget to update the name of a member." Of all the millions of places one might use an attribute, and forget to update it in the source code, only a tiny proportion will be guarded by a call to nameof. And even then, there will be cases -- quite common cases -- where one might forget to update the name inside the nameof() call, and not get an exception. For instance, I've frequently refactored names where I've ended up swapping two names. I can't think of an example off the top of my head right now, and I don't feel like trawling through years of history to find one, so I'll make one up: the variable "spam" is renamed to "eggs" and the "eggs" variable is renamed to "spam" Now if you refer to both: header = f"1:{nameof(spam)} 2:{nameof(eggs)}" # and some lines of code later on... line = f"1:{spam} 2:{eggs}" it's easy to swap the names in one place but not the other. I've done this more times than I care to admit to, and nameof() wouldn't have saved me. The bottom line is this: this has only a narrow use-case ("I need to print the name of a variable or attribute") and even in those use-cases, you still have to update the variable name in two places regardless of whether you write: # needs to be refactored to "eggs", eggs "spam", spam or # needs to be refactored to nameof(eggs), eggs nameof(spam), spam The second version may have the advantage that it will fail with an exception, instead of silently outputting the wrong string. But only sometimes. Which brings me back to my half tongue-in-cheek response about quoting variables with quote marks... as I see it, the easiest way to get this "nameof" operator to work is a source code transformation. The interpreter could simply do a compile-time replacement: nameof(spam) -> "spam" nameof(self.eggs) -> "eggs" nameof(None) -> "None" nameof(123) -> "123" or whatever semantics we desire. Seems like a lot of effort in adding this to the language, testing it, documenting it, dealing with the invariable problems when somebody uses "nameof" as a regular variable or function, etc etc etc, when we can just tell people: If you want the variable name as a string, use a string. And pay attention to what you are doing when you rename things. -- Steven _______________________________________________ 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/XP6SJGPTMBKCEVSYLCW6LTXU35XCGD5I/ Code of Conduct: http://python.org/psf/codeofconduct/