[Python-ideas] String formatting and namedtuple
Terry Reedy
tjreedy at udel.edu
Thu Feb 12 02:06:37 CET 2009
I am combining responses to several posts.
Guido van Rossum wrote:
>> [Raymond] I thought we had backed-off on this for a number of reasons.
>
> Can you refer me to a thread?
I believe there was one over a year ago on backing off from deprecation
in 3.0. I also believe the alternative time frame was subject to
differential interpretation.
***
> That probably means we'll still be supporting % 5 years from now.
Declare % interpolation 'frozen': no new features.
[As Raymond suggested with respect to attribute lookup.]
***
> Without deprecation as the stick I doubt that they will bother to
> even try it.
...
> .format() fixes several common stumbling blocks with %(name)s and at
> least one with %s.
If there is a concise article on "Advantages of .format over %
interpolation" to encourage use of the former, I am not aware of it.
Attribute lookup with field.name is one of many to promote.
One thing probably not mentioned in the PEP is the possibility of bound
methods, reduces typing of '.format' for reused formats.
>>> msg = "{0} == {1}".format
>>> print(msg('.format', 'improvement'))
.format == improvement
>>> msg('Python', 'greatness')
'Python == greatness'
[Steven D'Aprano]
>It's great for complex tasks, but it's hard to beat % for simple ones.
This seems to be a common feeling.
PROPOSAL: Allow the simple case to stay simple. Allow field names to be
omitted for all fields in a string and then default to 0, 1, ... so that
example above could be written as
>>> msg = "{} == {}".format
Given that computers are glorified counting machines, it *is* a bit
annoying to be required to do the counting manually. I think this is at
least half the objection to the new scheme!
Terry Jan Reedy
More information about the Python-ideas
mailing list