[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
- Previous message: [Python-Dev] PEP 461 - Adding % and {} formatting to bytes
- Next message: [Python-Dev] PEP 461 - Adding % and {} formatting to bytes
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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:
It could be hard to provide a helpful exception message since it is generated inside the format method rather than inside the bytes.mod method (in the case of a missing ascii method). The most common error will be using a str object and so we could modify the format method of str to provide a nice hint (use encode()).
Is there some risk that an object will unwittingly implement a format method that unintentionally accepts a bytes argument? That requires some investigation.
- Previous message: [Python-Dev] PEP 461 - Adding % and {} formatting to bytes
- Next message: [Python-Dev] PEP 461 - Adding % and {} formatting to bytes
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]