[Python-ideas] String formatting and namedtuple

Guido van Rossum guido at python.org
Thu Feb 12 19:46:00 CET 2009


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
>>>

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-ideas mailing list