[issue9856] Change object.__format__(s) where s is non-empty to a TypeError

Yogesh Chaudhari report at bugs.python.org
Mon May 13 00:06:22 CEST 2013


Yogesh Chaudhari added the comment:

>It's this case that is currently an error, but it need not be:
>>>> format(object(), '1')
>Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
>TypeError: non-empty format string passed to object.__format__

I believe that should continue to remain an error. Please note that this works
>>> format(object(), '')
'<object object at 0xb74fd688>'

>From what I can tell, specifying '1' or '2' or '100' makes no sense because unlike string or int (and like list or tuple ) this 'number' does not represent anything sensible.

This works fine as it should:
>>> format('q', '5')
'q    '
>>> format(1, '5')
'    1'
>>> format(1, '05')
'00001'
>>> 

But this does not and IMHO *should* not 'work'
>>> format([1,2,3], '5')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: non-empty format string passed to object.__format__
>>> format((1,2,3), '5')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: non-empty format string passed to object.__format__


an object() CANNOT have specific behavior like str or int. You can of-course argue as to what kind of error/exception/warning this may raise, but it does not make any sense (AFAIK) to 'fix' this.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9856>
_______________________________________


More information about the Python-bugs-list mailing list