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

Antoine Pitrou solipsis at pitrou.net
Mon Feb 24 00:04:33 CET 2014

On Mon, 24 Feb 2014 08:54:08 +1000
Nick Coghlan <ncoghlan at gmail.com> wrote:
> > > The idea being if we offer %a, folks won't be tempted to abuse
> __bytes__.
> >
> > Which folks are we talking about? This sounds gratuitous.
> It's a harm containment tactic, based on the assumption people *will* want
> to include the output of ascii() in binary protocols containing ASCII
> segments

But why would they? ascii() doesn't do what they want, since it's
repr()-like, not str()-like. It seems your assumption is wrong.

> By contrast, if %a doesn't exist, then it becomes more attractive to use %s
> in the format string and define an ASCII assuming  __bytes__ implementation
> on a custom type.

Uh... Few Python programmers would actually think of writing a __bytes__
method just to enable bytes interpolation for their custom types.
However, adding "%a" as a supported interpolation format just makes
things confusing for *everyone*.



More information about the Python-Dev mailing list