[issue33410] Using type in a format with padding causes TypeError

Eric V. Smith report at bugs.python.org
Wed May 2 13:06:33 EDT 2018


Eric V. Smith <eric at trueblade.com> added the comment:

The problem is that type.__format__ doesn't exist, so object.__format__ is being called, and it throws an error if you provide a format spec. This is done for future expansion: if we do want to add type.__format__ in the future, we don't have to worry about existing cases that are using format specs that might not work with the new type.__format__.

No format spec is the same as calling str() on the argument and returning that, which is what is happening in your working examples.

If you want to apply a str formatting spec, you should covert the argument to a str first, using either !s or str():

>>> print('{a!s: >10}'.format(a=type(a)))
<class 'str'>
>>> print('{a: >10}'.format(a=str(type(a))))
<class 'str'>

----------
nosy: +eric.smith

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue33410>
_______________________________________


More information about the Python-bugs-list mailing list