Eric V. Smith <[email protected]> added the comment:
I don't think "{}" is the correct way to document this. These all have an empty
format specifier:
"{}".format(foo)
"{:}".format(foo)
"{0}".format(foo)
"{0:}".format(foo)
"{name}".format(name=foo)
format(foo, "")
format(foo)
That is, they all call foo.__format__(""). If foo.__format__ (well, really
type(foo).__format__) doesn't exist, then object.__format__(foo, "") gets
called. It's object.__format__ that's checking for the empty format string, and
if so it returns str(foo).
What would you suggest changing the ':d' error message to, for objects that
don't support a format type of 'd'? This makes sense to me:
>>> format('', 'd')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Unknown format code 'd' for object of type 'str'
The problem, if there is one, is:
>>> format([], 'd')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Unknown format code 'd' for object of type 'str'
The problem is that the str that's producing this error doesn't know that it
exists because object.__format__ returned str([]).
----------
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue13790>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com