New Python 3.0 string formatting - really necessary?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Dec 19 21:25:37 EST 2008


On Fri, 19 Dec 2008 16:05:42 -0800, Scott David Daniels wrote:

> Ah, but for internationalization, you can change the format string to
> take args in a different order if, for example, French messages want
> modifiers on one side and English on the other. The code can stay the
> same, while only the text used to do the formatting must change.

I can't see how this is any different between what we have now. Here's an 
example using keyword arguments:

# untested
"{modifier} {noun}".format(modifier="dead", noun="parrot")
"{noun} {modifier}".format(modifier="mort", noun="perroquet")

compared to:

"%(modifier)s %(noun)s" % dict(modifier="dead", noun="parrot")
"%(noun)s %(modifier)s" % dict(modifier="mort", noun="perroquet")

Whether using % or format(), I don't see the need to change the code, 
only the strings.

Using positional arguments is not really that different:

"{0} {1}".format("dead", "parrot")
"{0} {1}".format("perroquet", "mort")

versus:

"%s %s" % ("dead", "parrot")
"%s %s" % ("perroquet", "mort")

In this case, the template on the left remains the same, you just have to 
reorder the string arguments on the right. Clearly less satisfactory than 
the solution using keyword substitution, but whatever solution you pick, 
I don't see any advantage to format() over % formatting. Can you show an 
example?



-- 
Steven



More information about the Python-list mailing list