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

Ethan Furman ethan at stoneleaf.us
Mon Feb 24 23:45:22 CET 2014

On 02/24/2014 02:33 PM, Nick Coghlan wrote:
> 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.

Jim Jewett had some use-cases that I'm happy to run with.  (Thanks jJ!)

So final question for %a:

%a can only be used in Python 3 (3.2+, I believe) -- do we want to be able to use %a as a short way of including text?

In Python2/3 code bases it will need to be '%s' % 'a string'.encode('ascii').

In Python 3 only code bases that could be shortened to '%a' % 'a string':

   pro: much easier
        if "mojibake" ( \x and \u sequences ) sneak in, the original data can still be retrieved

   cons: has surrounding quotes (would need to have bytes.__mod__ remove them)


More information about the Python-Dev mailing list