[Python-Dev] PEP 461: Adding % formatting to bytes and bytearray -- Final, Take 2

Nick Coghlan ncoghlan at gmail.com
Mon Feb 24 23:33:53 CET 2014

On 25 Feb 2014 05:44, "Antoine Pitrou" <solipsis at pitrou.net> wrote:
> On Mon, 24 Feb 2014 10:40:46 -0800
> Ethan Furman <ethan at stoneleaf.us> wrote:
> > Okay, types corrected, most comments taken into account.
> >
> > %b is right out, %a is still suffering scrutiny.
> >
> > The arguments seem to boil down to:
> >
> > We don't need it.
> >
> > vs
> >
> > Somebody might, and it's better than having them inappropriately add a
__bytes__ method if we don't have it.
> Don't forget that Python is a language for consenting adults. Adding a
> near-useless feature for fear that otherwise people might shoot
> themselves in the foot by using another feature must be one of the
> worst arguments I've ever heard :-)

That's not quite the argument. The argument is that __bytes__ is expected
to work in arbitrary binary contexts and hence should *never* assume ASCII
compatibility (the PEP should probably propose a new addition to PEP 8 to
that effect), so the question is then "OK, since it isn't defining
__bytes__, what is the preferred spelling for getting the ASCII compatible
representation of an object as a byte sequence?".

If we do nothing, then that spelling is "ascii(obj).encode('ascii')".

If %a is allowed as part of a binary interpolation pattern, then it becomes
"b'%a' % obj"

Allowing %a also improves the consistency with text interpolation. In the
case of %r, the inconsistency is based on needing to disallow arbitrary
Unicode code points in the result and not wanting to redefine %r as a
second way to spell %a. There's no corresponding reason to disallow %a -
the result is guaranteed to be ASCII compatible, so there's no risk of data
driven encoding errors, and no difference between doing the binary
interpolation directly, or doing text interpolation and then encoding the
result as ASCII.

As far as use cases go, as someone else mentioned, the main one is likely
to be binary logging and error reporting formats, as it becomes a quick and
easy way to embed a backslash escaped string. However, my interest is more
in providing an obvious way to do it and in minimising the differences
between text and binary interpolation.


> Regards
> Antoine.
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20140225/1fa1a6a8/attachment.html>

More information about the Python-Dev mailing list