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

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 hell.

Thanks for writing the PEP.

Neil



More information about the Python-Dev mailing list