[Python-Dev] transitioning from % to {} formatting
Guido van Rossum
guido at python.org
Wed Sep 30 19:37:20 CEST 2009
On Wed, Sep 30, 2009 at 9:48 AM, "Martin v. Löwis" <martin at v.loewis.de> wrote:
>> I'd note that PEP 3101 calls str.format() a replacement for
>> %-formatting, not an alternate mechanism to achieve the same end.
>
> I think this is a mis-wording; the intent of the PEP apparently is
> to propose this mechanism as an option, not as an actual replacement.
> This becomes clear when reading the "Backwards Compatibility" section:
The problem is, PEP 3101 and our interpretation of it evolved. The
original proposal for {}-formatting was certainly put forward with the
aim to completely *replace* %-formatting, and care was taken in the
design to cover all use cases, avoid known problems, etc.
Then we started looking seriously at conversion from Python 2 to
Python 3 and we discovered that converting %-formatting to
{}-formatting was a huge can of worms, and decided it wasn't worth to
try and do *at the time* given the Python 3 schedule. We considered
some kind of gentle deprecation warning, but decided that even that
would be too noisy. So now we have two competing mechanisms.
In the long run, say Python 4, I think we don't need both, and we
should get rid of one. My preference is still getting rid of
%-formatting, due to the problems with it that prompted the design of
{}-formatting (no need to reiterate the list here).
So how do we get there? My proposal would be to let this be a gradual
take-over of a new, superior species in the same niche as an older
species. (Say, modern man over Neanderthal man.) Thus, as new code is
written (especially example code, which will be copied widely), we
should start using {}-formatting, and when new APIs are designed that
tie in to some kind of formatting, they should use {}-formatting.
Adding support for {}-formatting, in addition to %-formatting, to
existing APIs like the logging package also strikes me as a good idea,
as long as backwards compatibility can be preserved. (I have no strong
ideas on how to do this right now.)
If we do this right, by the time Python 4 comes around, {}-formatting
will have won the race, and there won't be a question about removing
%-formatting at the time. I wouldn't be surprised if by then static
analysis techniques will have improved so that we *can* consider
automatic conversion by then.
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev
mailing list