[Python-Dev] transitioning from % to {} formatting

Georg Brandl g.brandl at gmx.net
Sat Oct 3 11:22:04 CEST 2009

Bugbee, Larry schrieb:
>>> Do the users get any say in this?
>> I'm a user! :-)
>> I hate calling methods on string literals, I think it looks very odd to
>> have code like this:
>> "Displaying {0} of {1} revisions".format(x, y)
> Ugh!  Good point.
> Is Python to be an easy-to-learn-and-remember language?  I submit we are
> losing that one.  To a user, this will be confusing.  To a C programmer
> coming over to Python, especially so.  Some of what makes %-formatting easy
> to remember is its parallel in C.

To a .NET programmer coming to Python (think IronPython), %-formatting will
be strange, while {}-style formatting will be familiar.  I don't know how many
programmers come to Python from C nowadays.

I also think that we are merely so accustomed to ``"..." % bar`` that we never
think about new users who go "Huh? String modulo string?" -- why should that
initial discomfort be less than that of calling a method on a literal?

Being able to call a method on literals is simply consistent; literals are
objects as well as an object referred to by a name.  There are languages where
you can't even call ``foo[bar].baz()`` because method calls only work on single
names.  I call that ugly.

> I'm conflicted.  Philosophically I like the idea of mnemonic names over
> positional variables and allowing variable values determined elsewhere to be
> inserted in print strings.  It is appealing.
> Unless the benefit is at least 2x, a change should not be made,

I don't see how numbers like "2x" can be applied when measuring the
benefit of a language feature.

> and I don't
> think this benefit rises to where it is worth the confusion and problems.
> ...and converting the legacy base.  And forget pretty, not that %-formatting
> is pretty either.  Besides, according to the bench, it is slower too.  And it
> will take editors a while before the new syntax is supported and colorized,
> thus some errors for a while.

This is no different from all other language features.  Some editors I
encountered do not yet color the with statement.  Should I therefore refrain
from using it?

Highlighting formatting placeholders in string literals is questionable anyway,
since a highlighter has no way of knowing whether a given string literal will
be used for formatting.

> ....and if one wants a "{" or a "}" in the printed output, one has to escape
> it?  That is -2x over wanting a "%" in the output.

Again a number.  Where does it come from?


Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.
Four shall be the number of spaces thou shalt indent, and the number of thy
indenting shall be four. Eight shalt thou not indent, nor either indent thou
two, excepting that thou then proceed to four. Tabs are right out.

More information about the Python-Dev mailing list