Finding the instance reference of an object

Duncan Booth duncan.booth at invalid.invalid
Fri Oct 31 17:02:53 CET 2008


pjacobi.de at googlemail.com wrote:

>> x = 1
>> y = x  # does assignment make copies?
>> y += 1
>> assert x == 1
>> => succeeds, which implies that Python makes a copy when assigning
> 
> with lists:
> 
>> x = [1]
>> y = x  # does assignment make copies?
>> y += [1]
>> assert x == [1]
>> => fails, which implies that Python uses references when assigning
> 
> Compare lists with tupels:
> 
> x = (1,)
> y = x  # does assignment make copies?
> y += (1,)
> assert x == (1,)
>=> succeeds, which implies *what*?

All any of this does is 'implies that += may create a new object or may 
mutate an existing object. RTFM: Python Reference Manual 6.3.1

"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."

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list