PATCH: Augmented assignment

Huaiyu Zhu hzhu at rocket.knowledgetrack.com
Fri Jun 9 15:16:28 EDT 2000


>>As promised, here is my patch to add augmented assignment (+=, -= and
>>family) to Python. It's still Work In Progress, and probably requires a
>>rewrite of some magnitude before Guido will accept it.
>>

Greate!  I'll use them in the MatPy package as well.

BTW, maybe __add_eq__ would be a better name than __add_ad__, as it
describes the symbols instead of their meaning.


>Why would the Python community want this?  It obfuscates code, in as much
>that newbies aren't likely to intuite what the symbols mean.
> 

Which of the following is clearer to a newbie?  

dictionary[key][index] = dictionry[key][index] + 1
dictionary[key][index] += 1

The first happens to have a typo as well. Besides, the second version is
always faster. For many classes the speed gain can be enormous, as obj=obj+1
is likely to involve a "partitially deep" copy.

The new patch gives ability to define __add_ad__ independently of __init__
and __add__ so that obj+=1 takes advantage of potential efficiency gain.


Huaiyu



More information about the Python-list mailing list