[Python-Dev] PEP 461 - Adding % and {} formatting to bytes

Brett Cannon brett at python.org
Thu Jan 16 15:45:14 CET 2014


On Thu, Jan 16, 2014 at 2:51 AM, Ethan Furman <ethan at stoneleaf.us> wrote:

> On 01/15/2014 06:45 AM, Brett Cannon wrote:
>
>>
>> This is why I have argued that if you specify it as "if there is a format
>> spec specified, then the return value from
>> calling __format__() will have str.decode('ascii', 'strict') called on
>> it" you get the support for the various
>> number-specific format specs for free.
>>
>
> It may work like this under the hood, but it's an implementation detail.


I'm arguing it's not an implementation detail but a definition of how
bytes.format() would work.


>  Since the numeric format codes will call int, index, or float on the
> object (to handle subclasses),


But that's **only** because the numeric types choose to as part of their
__format__() implementation; it is not inherent to str.format().


> we could then call __format__ on the resulting int or float to do the
> heavy lifting;


It's not just the heavy lifting; it does **all** the lifting for format
specifications.


> but since __format__ on anything else would never be called I don't want
> to give that impression.
>
>
Fine, if you're worried about bytes.format() overstepping by implicitly
calling str.encode() on the return value of __format__() then you will need
__bytes__format__() to get equivalent support.

-Brett


>
>  It also means if you pass in a string that you just want the strict ASCII
>> bytes
>> of then you can get it with {:s}.
>>
>
> This isn't going to happen.  If the user wants a string to be in the byte
> stream, it has to either be a bytes literal or explicitly encoded [1].
>
> --
> ~Ethan~
>
> [1] Apologies if this has already been answered.  I wanted to make sure I
> responded to all the ideas/objects, and I may have responded more than once
> to some.  It's been a long few threads.  ;)
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/
> brett%40python.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20140116/a626f880/attachment.html>


More information about the Python-Dev mailing list