Python 2.0b1 is released!

John W. Baxter jwbnews at scandaroon.com
Wed Sep 6 16:53:16 CEST 2000


In article <Pine.LNX.4.21.0009061312290.24593-100000 at fep132.fep.ru>, 
phd at mail2.phd.pp.ru wrote:

> On 6 Sep 2000, Bernhard Reiter wrote:
> > >   - Augmented assignment, e.g. x += 1
> > 
> > I cannot resist saying that I still consider this an archane "C" -like
> > feature which should be better solved with the replace or macro
> > function of the editor. I always liked the clearness of the normal
> > 	x = x + 1
> 
>    Do you prefer
> dict["key"].attr = dict["key"].attr + 12
>    instead of
> dict["key"].attr += 12
>    ?
> 
>    The second is cleaner and faster, right?
> 

Many people think the augmented assignment form is cleaner.  (I don't 
happen to be one of them...I'd probably do the above with += in throw 
away code but not in a real project.  (Unless it really is faster and is 
executed a lot.)  One can offer more horrendous examples in which I 
would use the augmented assignment form--or do something else entirely.

It may or may not be faster...it (that is, the C equivalent, which means 
something other than a dictionary) hasn't been faster in decent C 
implementations in many years.  It *was* faster on a particular 
processor [PDP-11] (and probably others) with early C compiler 
implementations.  [Check the generated code...these days the two forms 
tend to produce the same code.]

But there is a good chance that in the above Python example the 
augmented assignment is indeed faster.

And, I suspect, this sort of thing doesn't change (today's challenge:  
come up with a problem statement in which the following makes sense ;-)):

dict["key"] = dict.get("key", 244) + 12

   --John

-- 
John W. Baxter   Port Ludlow, WA USA  jwbnews at scandaroon.com



More information about the Python-list mailing list