[Tutor] 2 vs 3

Steven D'Aprano steve at pearwood.info
Mon Sep 7 13:58:27 CEST 2015


On Mon, Sep 07, 2015 at 11:01:00AM +0100, Alan Gauld wrote:
> On 07/09/15 10:32, Laura Creighton wrote:
> >In a message of Mon, 07 Sep 2015 10:16:16 +0100, Alan Gauld writes:
> >
> >>And of course if you are really moving to the v3 way of
> >>doing things you should move to using the new string
> >>formatting style.
> >
> >I don't think there is any great push to prefer the new style over
> >the old style 'just because' when you are doing simple things.
> 
> No 'big push' because there's too much historic code out there.
> But I thought I'd read somewhere when it first came out that
> the old style was deprecated? But I may be hallucinating! :-)

When the format() method first came out, there was talk about 
deprecating % formatting, but it was only talk and never became 
official.

> OTOH the new style brings enough benefits, especially in the
> areas of dynamic formatting,  that I'd strongly suggest moving
> to it if you are making the jump to v3 anyway!

It's worth learning both formatting systems, even if you're just using 
Python 2. The format method started in 2.6, and 2.7 added the 
"autonumbering" feature that makes it much easier to use:

"{0} {1} {2}".format(a, b, c)  # needed in 2.6
"{} {} {}".format(a, b, c)  # 2.7 and up


Having said that, I still don't know how to use format other than the 
basics, as most of the code I write has to be compatible with 2.4 
onwards.



-- 
Steve


More information about the Tutor mailing list