On Tue, 2011-10-25 at 10:36 +1100, Steven D'Aprano wrote:
Guido van Rossum wrote:
On Sat, Oct 22, 2011 at 10:23 PM, Steven D'Aprano email@example.com wrote:
Nick Coghlan wrote: [...]
The current convention is that classes, functions and modules, don't offer a shorthand "pretty" display format at all. The proposal is to specifically bless "x.__name__" as an official shorthand.
I believe you are saying that backwards.
The way to get the shorthand display format (i.e. the object's name) is already to use x.__name__. There's no need for a proposal to bless x.__name__ as the way to do it since that's already what people do.
This proposal is to bless str(x) (and equivalent forms) as a shorthand for x.__name__, and *unbless* x.__name__ as the official way to do it. I expect that's what you mean.
That is putting words in my mouth. There's no intent to unbless x.__name__, period. Also, there's no intent to bless str(x) as x.__name__ if you want the name. The only intent is to make what gets printed if you print a function, class or module to be less verbose.
I'm sorry about that, I was describing the proposal as best I understood it.
Once this change goes ahead, under what circumstances would you expect people to continue using cls.__name__ (other than for backwards compatibility)? When beginners ask me "how do I get the name of a class?", what answer should I give?
This isn't a big as change as you may be thinking of.
name = cls.__name__
Won't change. You will still need to use the __name__ attribute to get a name in almost all situations except for when you want to place the name in a string or print it, you can then use just the cls, as the __str__ method would do the rest for you.
For example, I have code that does things like this:
raise TypeError('expected a string but got %s' % type(arg).__name__)
In the future, I expect that should be written like this:
raise TypeError('expected a string but got %s' % type(arg))
Yes, it could be written that way, but it's not required.
That's all I meant by "unbless". I didn't mean to imply that __name__ would go away, only that it would cease to be the One Obvious Way to get the name. If I'm wrong about this, then I'm genuinely confused and don't understand the motivation for this change.
Printing the name of an exception, class, or function, is very common for logging and error messages, and this makes that easier and cleaner. And if a repr is wanted, we still have that also.