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

Andrew Barnert abarnert at yahoo.com
Wed Sep 9 23:50:16 CEST 2015

On Sep 9, 2015, at 09:05, Sven R. Kunze <srkunze at mail.de> wrote:
>> On 09.09.2015 02:09, Andrew Barnert via Python-ideas wrote:
>> I think it's already been established why % formatting is not going away any time soon.
>> As for de-emphasizing it, I think that's already done pretty well in the current docs. The tutorial has a nice long introduction to str.format, a one-paragraph section on "old string formatting" with a single %5.3f example, and a one-sentence mention of Template. The stdtypes chapter in the library reference explains the difference between the two in a way that makes format sound more attractive for novices, and then has details on each one as appropriate. What else should be done?
> I had difficulties to find what you mean by tutorial. But hey, being a Python user for years and not knowing where the official tutorial resides...

If you go to docs.python.org (directly, or by clicking the link to docs for Python 3 or Python 2 from the home page or the documentation menu), Tutorial is the second thing on the list, after What's New. And, as you found, it's the first hit for "Python tutorial" on Google.

At any rate, if you're not concerned with the tutorial, which parts of the docs are you worried about? Sure, a lot of people learn Python from various books, websites, and classes that present % instead of (or at least in equal light with) {}, but those are all outside the control of Python itself. You can't write a PEP to get the author of ThinkPython, a guy who wrote 1800 random StackOverflow answers, or the instructor for Programming 101 at Steve University to change what they teach.

And if not the docs, what else would it mean to "de-emphasize" %-formatting without deprecating or removing it?

> Anyway, Google presented me the version 2.7 of the tutorial.

That's a whole other problem. But nobody is going to retroactively change Python 2.7 just to help people who find the 2.7 docs when they should be looking for 3.5.

That might seem reasonable today, when 2.7 could heartily recommend str.format because it's nearly the same in 2.7 as in 3.5, but what about next year, when f-strings are the preferred way to do it in 3.6? If 3.6 de-emphasizes str.format (as a feature used only when you need backward compat and/or dynamic formats) and its tutorial, %-formatting docs, and str.format docs all point to f-strings, having 2.7's docs point people to str.format will be misleading at best for 3.6, but having it recommend something that doesn't exist in 2.7 will be actively wrong for 2.7.

The solution is to get people to the 3.5 or 3.6 docs in the first place, not to hack up the 2.7 docs.

> I still don't understand what's wrong with deprecating %, but okay.

Well, have you read the answers given by Nick, me, and others earlier in the thread? If so, what do you disagree with? You've only addressed one point (that % is faster than {} for simple cases--and your solution is just "make {} faster", which may not be possible given that it's inherently more hookable than % and therefore requires more function calls...). What about formatting headers for ASCII wire protocols, sharing tables of format strings between programming languages (e.g., for i18n), or any of the other reasons people have brought up?

More information about the Python-ideas mailing list