On 22 October 2011 21:32, Guido van Rossum <guido@python.org> wrote:
On Sat, Oct 22, 2011 at 1:18 PM, Steven D'Aprano <steve@pearwood.info> wrote:
> 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].
> http://docs.python.org/reference/datamodel.html#object.__str__
> 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].
> http://docs.python.org/library/functions.html#str
> 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.

Thinking of str(x) as an API to get a certain value would lead there,
yes. But thinking of str(x) as what gets printed by print(x),
formatted by "{}.format(x)", and "%s" % s, changes things. When I am
printing an object and I have no idea what type it is, I'll use repr()
or "%r"; but when I know I am printing, say, an exception, I think it
would be very nice if print(x) would just print its name. Just like
print(None) prints 'None', it would make all the sense in the world if
print(ZeroDivisionError) printed 'ZeroDivisionError', and
print(type(42)) printed 'int'.



--Guido van Rossum (python.org/~guido)
Python-ideas mailing list


May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html