I hit this TypeError exception recently, which I found very difficult to debug. I eventually reduced it to this small test case:
>>> "{:20}".format(b"hi")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: non-empty format string passed to object.__format__
This is very non-obvious, to me anyway. The workaround for my code was to decode the byte string into unicode:
>>> "{:20}".format(b"hi".decode("ascii"))
'hi '
What is the meaning of this exception? Is there a way it can be made more clear?
TypeErrorin Python 3.4+, and it affects anything that inherits fromobjectwithout defining__format__along the way (e.g.None;class T(object): pass, etc.).None. Thus'{0:.4f}'.format(bla)caused this exception