[Python-Dev] PEP 461 - Adding % and {} formatting to bytes (original) (raw)

Neil Schemenauer nas at arctrix.com
Thu Jan 16 18:13:43 CET 2014


Michael Urman <murman at gmail.com> wrote:

If format receives a formatspec of type bytes, it should return bytes. For such cases on objects that cannot support bytes (i.e. for str), it can raise. This appears to avoid the need for additional methods. (As does Nick's proposal of leaving it out for now.)

That's an interesting idea. I proposed ascii as a analogous method to format for bytes formatting and to have %-interpolation use it. However, overloading format based on the type of the argument could work.

I see with Python 3:

>>> (1).__format__(b'')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str, not bytes

A TypeError exception is what we want if the object does not support bytes formatting. Some possible problems:



More information about the Python-Dev mailing list