Yet another Python textbook

Joshua Landau joshua.landau.ws at gmail.com
Thu Nov 22 17:56:59 EST 2012


On 22 November 2012 22:41, Colin J. Williams <cjw at ncf.ca> wrote:

> On 22/11/2012 1:27 PM, Ian Kelly wrote:
>
>> On Thu, Nov 22, 2012 at 5:24 AM, Colin J. Williams <cjw at ncf.ca> wrote:
>>
>>>  From my reading of the docs, it seems to me that the three following
>>> should
>>> be equivalent:
>>>
>>>    (a) formattingStr.format(values)
>>> with
>>>    (b) format(values, formattingStr)
>>> or
>>>    (c) tupleOfValues.__format__(**formattingStr
>>>
>>> Example:
>>> print('{:-^14f}{:^14d}'.**format(-25.61, 95 ))
>>> print(format((-25.61, 95), '{:-^14f}{:^14d}'))
>>> (-25.61, 95 ).__format__('{:-^14f}{:^14d}'**)
>>>
>>> The second fails, perhaps because values can only be a single value.
>>> The third fails, the reason is unclear.
>>>
>>
>> The latter two (which are more or less equivalent) fail because they are
>> intended for invoking the formatting rules of a single value.  The
>> string argument to each of them is not a format string, but a "format
>> specification", which in a format string is only the part that goes
>> inside the curly braces and after the optional colon.  For example, in
>> this format string:
>>
>
> Thanks, this is clear.  I wish the docs made this clearer.
>
> You and I used __format__.  I understand that the use of double underscore
> functions is deprecated.  Is there some regular function which can achieve
> the same result?


>>> help(format)
format(...)
    format(value[, format_spec]) -> string

    Returns value.__format__(format_spec)
    format_spec defaults to ""


 *In other words, "format(a, b)" is the correct way to write
"a.__format__(b)".*

This is in the same way that "a.__add__(b)" should be written "a + b", or
"a.__round__(b)" written "round(a, b)".
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121122/ba8df180/attachment.html>


More information about the Python-list mailing list