[Python-Dev] Sort out formatting differences in decimal and float

Terry Reedy tjreedy at udel.edu
Sun Dec 6 02:35:08 CET 2009


Nick Coghlan wrote:
> skip at pobox.com wrote:
> 
>>  My
>> apologies if I don't understand some amazing generality about format()
> 
>>>> format("Header", '=^20s')
> '=======Header======='
> 
>>>> "Format a single object {0!r}, multiple times in a single string.
> Year: {0.year}; Month: {0.month}; Day: {0.day}; Formatted:
> {0:%Y-%M-%d}".format(datetime.now())
> 'Format a single object datetime.datetime(2009, 12, 6, 9, 16, 0,
> 875018), multiple times in a single string. Year: 2009; Month: 12; Day:
> 6; Formatted: 2009-16-06'
> 
>>>> "Use keyword arguments easily: {x}, {y}, {z}".format(x=1, y=2, z=3)
> 'Use keyword arguments easily: 1, 2, 3'
> 
> For the things that mod formatting already allows you to do, our aim is
> to get format() functionality at least on par with what mod formatting
> supports (it should be most of the way there with the number formatting
> cleanups for 2.7/3.2). For the rest of the features (explicit position
> references, centre alignment, arbitrary fill characters, attribute and
> subscript references, type defined formatting control), mod formatting
> isn't even in the game.
> 
> Getting rid of the magic behaviour associated with the use of tuples on
> the right hand side is also valuable:
> 
>>>> "%s" % (1, 2)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: not all arguments converted during string formatting
>>>> "{}".format((1, 2))
> '(1, 2)'

A nice demonstration of what an excellent piece of work the new .format 
is or is becoming. I would still like it to be a goal for 3.2 that all 
stdlib modules that work with formats accept the new formats and not 
just % formats.

Mark Summerfield only covered .format in his book on Python 3 programimg 
and I hated to tell him that there was at least one module in the stdlib 
that currently (3.1) requires the old style.

Terry Jan Reedy



More information about the Python-Dev mailing list