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

R. David Murray rdmurray at bitdance.com
Thu Mar 27 14:48:54 CET 2014


On Thu, 27 Mar 2014 12:24:49 +0100, Antoine Pitrou <solipsis at pitrou.net> wrote:
> On Tue, 25 Mar 2014 15:37:11 -0700
> Ethan Furman <ethan at stoneleaf.us> wrote:
> > 
> > ``%a`` will call ``ascii()`` on the interpolated value.  This is intended
> > as a debugging aid, rather than something that should be used in production.
> > Non-ASCII values will be encoded to either ``\xnn`` or ``\unnnn``
> > representation.  Use cases include developing a new protocol and writing
> > landmarks into the stream; debugging data going into an existing protocol
> > to see if the problem is the protocol itself or bad data; a fall-back for a
> > serialization format; or even a rudimentary serialization format when
> > defining ``__bytes__`` would not be appropriate [8].
> 
> The "use cases" you are enumerating for "%a" are chimeric. Did you
> *actually* do those things in real life, or are you inventing them for
> the PEP?

The use cases came from someone else (Jim Jewett?) so you should
be asking him, not Ethan :)

As for the "did you actually do those things in real life", I know I've
done the "dump the repr into the data (protocol) stream to see what
I've really got here" debug trick in the string context, so I have no
doubt that I will want to do it in the bytes context as well.  In fact,
it is probably somewhat more likely in the bytes context, since I know
I've been in situations with data exchange protocols where I couldn't
get console output and setting up logging was much more painful than
just dumping the debug data into into the data stream.  Or where doing
so made it much clearer what was going on than separate logging would.
I've done the 'landmark' thing as well, in the string context; that can be
very useful when doing incremental test driven development.  (Granted, you
could do that with __bytes__; you might well be writing a __bytes__
method anyway as the next step, but it *is* more overhead/boilerplate than
just starting with %a...and it gets people used to reaching for __bytes__
for the "wrong" purpose, which is Nick's concern).  In theory I can see
using %a for serialization in certain limited contexts (I've done that
with string repr in private utility scripts), but in practice I doubt
that would happen in a binary context, since those are much more likely
to be actually going over a "wire" of some sort (ie: places you really
don't want to use eval even when it would work).

So yeah, I think %a has *practical* utility.

--David


More information about the Python-Dev mailing list