[Python-Dev] PEP 461 - Adding % and {} formatting to bytes

Neil Schemenauer nas at arctrix.com
Wed Jan 15 16:47:43 CET 2014

This looks pretty good to me.  I don't think we should limit
operands based on type, that's anti-Pythonic IMHO.  We should use
duck-typing and that means a special method, I think.  We could
introduce a new one but __bytes__ looks like it can work.
Otherwise, maybe __ascii__ is a good name.

Objects that implement __str__ can also implement __bytes__ if they
can guarantee that ASCII characters are always returned, no matter
what the *value* (we don't want to repeat the hell of Python 2's
unicode to str coercion which depends on the value of the unicode
object).  Objects that already contain encoded bytes or arbitrary
bytes can also implement __bytes__.

Ethan Furman <ethan at stoneleaf.us> wrote:
> %s, because it is the most general, has the most convoluted resolution:

This becomes much simpler:

     - does the object implement __bytes__?
       call it and use the value otherwise raise TypeError

> It has been suggested to use %b for bytes instead of %s.
>    - Rejected as %b does not exist in Python 2.x %-interpolation, which is
>      why we are using %s.

+1.  %b might be conceptually neater but ease of migration trumps
that, IMHO.

> It has been proposed to automatically use .encode('ascii','strict') for str
> arguments to %s.
>    - Rejected as this would lead to intermittent failures.  Better to have the
>      operation always fail so the trouble-spot can be correctly fixed.

Right.  That would put us back in Python 2 unicode -> str coercion

Thanks for writing the PEP.


More information about the Python-Dev mailing list