[Python-ideas] String formatting and namedtuple

Terry Reedy tjreedy at udel.edu
Thu Feb 12 20:24:25 CET 2009

Guido van Rossum wrote:
> On Thu, Feb 12, 2009 at 10:29 AM, David Borowitz <ddborowitz at gmail.com> wrote:
>> Can't roughly the same thing be achieved with % substitution?
>>>>> msg = '%s == %s'
>>>>> print(msg % ('.format', 'improvement'))
>> .format == improvement
>>>>> msg % ('Python', 'greatness')
>> 'Python == greatness'
>> The main non-syntactic difference here is that msg is a normal string object
>> rather than a bound method.
>> Not arguing against moving to .format, just that it doesn't seem inherently
>> more powerful than % in this case. (I guess you could still argue that this
>> pattern alleviates a concern with .format, namely repeated typing of
>> .format, but that's never been an issue with % to begin with.)
> It has the classic issue that % always has: if there's a single %s in
> the format string it will do the wrong thing when the argument is a
> tuple.
>>>> s = "[%s]"
>>>> mystery_object = ()
>>>> s % mystery_object
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: not enough arguments for format string

Using bound methods was, to me, a lesser point of my post.  I still am 
curious what anyone thinks of

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 switching to .format.

Terry Jan Reedy

More information about the Python-ideas mailing list