On Sat, Oct 22, 2011 at 1:18 PM, Steven D'Aprano <firstname.lastname@example.org
> Nick Coghlan wrote:
>> If someone wants debugging level detail, use repr(), just like the
>> interactive interpreter does.
> I'm just going to repeat what I've said before: explicit is better than
> implicit. If you want the name of an object (be it a class, a module, a
> function, or something else), you should explicitly ask for the name, and
> not rely on its str().
> The details returned by str() are, in some sense, arbitrary. The docs
> describe it as [quote] the “informal” string representation of an object
> [end quote].
> On that basis, objects are free to return as much, or as little, information
> as makes sense in their str(). (As you pointed out earlier.)
> However, the docs also say that str() should return [quote] a string
> containing a nicely printable representation of an object [end quote].
> To my mind, the name alone of a class (or function or module) is in no sense
> a nicely printable representation of the object. I would argue strongly that
> the property of being "nicely representable" outweighs by far the
> convenience of avoiding 9 extra characters in one specific use-case:
> "blah blah blah class '%s'" % cls # instead of cls.__name__
> But for the sake of the argument, I'll grant you that we're free to change
> str(cls) to return the class name, as requested by the OP, or the fully
> qualified module.class dotted name as suggested by you. So let's suppose
> that, after a long and bitter debate over which colour to paint this
> bikeshed, you win the debate.
> But this doesn't help you at all, because you can't rely on it. It seems to
> me that the exact format of str(cls) is an implementation detail. You can't
> rely on other Pythons to do the same thing, nor can you expect a guarantee
> that str(cls) won't change again in the future. So if you care about the
> exact string that gets generated, you still have to explicitly use
> cls.__name__ just as you do now.
> The __name__ attribute is part of the guaranteed API of class objects (and
> also functions and modules), the output of str(cls) is not. In my opinion
> relying on it to return a particular output is dangerous, regardless of
> whether the output is "<class 'module.MyClass'>", "module.MyClass",
> "MyClass" or something else.
> Having str(cls) return just the class name (or the module.class dotted name)
> is an attractive nuisance that should be resisted.