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.
">

(original) (raw)




On Thu, Jan 16, 2014 at 2:51 AM, Ethan Furman <ethan@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@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: https://mail.python.org/mailman/options/python-dev/brett%40python.org