[Python-ideas] One way to do format and print

Andrew Barnert abarnert at yahoo.com
Mon Sep 7 23:00:19 CEST 2015


On Sep 7, 2015, at 11:57, Sven R. Kunze <srkunze at mail.de> wrote:
> 
>> On 07.09.2015 19:39, Stefan Krah wrote:
>> Sven R. Kunze <srkunze at ...> writes:
>>>>       print("Hello, I am %s. My favorite number is %s." % (b, a))
>>>> 
>>>> ... and this is the other. Being available for non-literals means they
>>>> can be used with i18n, string tables, and other transformations.
>>>> Percent formatting is similar to what other C-derived languages have,
>>> Still necessary? Really, really necessary? Or just because we can?
>> Absolutely. For many Python users this is the preferred form. I find
>> that of all variations, this one is the most readable.
> 
> Okay, convinced. ;)
> 
> No, seriously, what would you do when Python would deprecate % syntax? Could you switch to {} ?

There's some confusion over this because the str.format proposal originally suggested deprecating %, and there are still some bloggers and StackOverflow users and so on that claim it does (sometimes even citing the PEP, which explicitly says the opposite). But there will always be cases that % is better for, such as:

 * sharing a table of format strings with code in C or another language
 * simple formats that need to be done fast in a loop
 * formatting strings to use as str.format format strings
 * messages that you've converted from logging to real output
 * ASCII-based wire protocols or file formats

So, even if it weren't for the backward compatibility issue for millions of lines of old code (and thousands of stubborn old coders), I doubt it would ever go away. At most, the tutorial and other docs might change to de-emphasize it and make it seem more like an "expert" feature only useful for cases like the above (as is already true for string.Template, and may become true for both % and str.format after f-strings reach widespread use--but nobody can really predict that until f-strings are actually in practical use).

TOOWTDI is a guideline that has to balance against other guidelines, not a strict rule that always trumps everything else, and unless someone can come up with something new that's so much better than both format and % that it's clearly worth overcoming the inertia rather than just being a case of the old standards joke (insert XKCD reference here), there will be two ways to do this.


More information about the Python-ideas mailing list