[Edu-sig] class notes (post facto)

Vern Ceder vceder at canterburyschool.org
Wed Jul 14 05:16:56 CEST 2010


Litvin wrote:
> At 01:54 AM 7/13/2010, kirby urner wrote:
>> You mean some methods actually change an object
>> "in place"?  Change it's state?  No copies, no
>> output?  No "back up"?
> 
> I've found it disconcerting that in Python 3
> lst1 += lst2 changes lst1 in place, while
> lst1 = lst1 + lst2 makes a copy.

Actually this behavior has been in Python since augmented assignments 
were introduced in version 2.0. The official wording on this (buried in 
the language reference) is (2.7 version, emphasis mine):

"An augmented assignment expression like x += 1 can be rewritten as x = 
x + 1 to achieve a similar, *but not exactly equal* effect. In the 
augmented version, x is only evaluated once. Also, when possible, *the 
actual operation is performed in-place*, meaning that rather than 
creating a new object and assigning that to the target, the old object 
is modified instead."

It *is* a bit disconcerting, but I can't at the moment think of a likely 
scenario where it would be a real problem.

Cheers,
Vern

> 
> Gary Litvin
> www.skylit.com
> 
-- 
This time for sure!
    -Bullwinkle J. Moose
-----------------------------
Vern Ceder, Director of Technology
Canterbury School, 3210 Smith Road, Ft Wayne, IN 46804
vceder at canterburyschool.org; 260-436-0746; FAX: 260-436-5137

The Quick Python Book, 2nd Ed - http://bit.ly/bRsWDW


More information about the Edu-sig mailing list