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

Nick Coghlan ncoghlan at gmail.com
Sun Dec 6 00:28:29 CET 2009

skip at pobox.com wrote:

>  My
> apologies if I don't understand some amazing generality about format()

>>> format("Header", '=^20s')

>>> "Format a single object {0!r}, multiple times in a single string.
Year: {0.year}; Month: {0.month}; Day: {0.day}; Formatted:
'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)'

Developers that are already used to the limitations of mod formatting
are expected to take some time to decide if they care about the extra
features offered by the format method, but for new developers it should
be an easy choice.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-Dev mailing list