[Python-ideas] Changing str(someclass) to return only the class name

Steven D'Aprano steve at pearwood.info
Tue Oct 25 01:36:32 CEST 2011


Guido van Rossum wrote:
> On Sat, Oct 22, 2011 at 10:23 PM, Steven D'Aprano <steve at pearwood.info> 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?

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))

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.



-- 
Steven




More information about the Python-ideas mailing list