[Python-Dev] PEP 460: allowing %d and %f and mojibake

Ethan Furman ethan at stoneleaf.us
Sat Jan 11 22:50:59 CET 2014


On 01/11/2014 10:32 AM, Antoine Pitrou wrote:
> On Sat, 11 Jan 2014 18:41:49 +0100
> Victor Stinner <victor.stinner at gmail.com> wrote:
>>
>> b'x=%s' % 10 is well defined, it's pure bytes.
>
> It is well-defined? Then please explain me what the general case of
>    b'%s' % x
> is supposed to call:

This is the key question, isn't it?


> - does it call x.__bytes__? int.__bytes__ doesn't exist

Perhaps that's the problem.  According to the docs:
========================================================================
  object.__bytes__(self)

     Called by bytes() to compute a byte-string representation of an object. This should return a bytes object.
========================================================================

Obviously, with the plethora of different binary possibilities for representing a number (how many bytes? endianness? 
which complement?), we would be well within our rights to decide that the "byte-string representation" of the numeric 
types is the ASCII equivalent of their __repr__ or __str__, and implement __bytes__ appropriately for them.  Any other 
object that wants to be represented easily in a byte stream would also have to implement __bytes__.   If necessary we 
could add __bytes__ to str for /strict/ ASCII conversion (even latin-1 would have to be explicitly encoded)[1].

--
~Ethan~

[1] I'm iffy on this point as I'm not at all sure it's needed.


More information about the Python-Dev mailing list