Problem with assignment. Python error or mine?

duncan smith duncan at invalid.invalid
Thu Dec 21 14:27:42 EST 2017


On 21/12/17 19:06, John Ladasky wrote:
> On Thursday, December 21, 2017 at 7:37:39 AM UTC-8, MRAB wrote:
> 
>> Python never makes a copy unless you ask it to.
>>
>> What x1=X does is make the name x1 refer to the same object that X 
>> refers to. No copying.
> 
> Well, except with very simple, mutable data types like scalars... compare this:
> 
>>>> x=5
>>>> y=x
>>>> x,y
> (5, 5)
>>>> x+=1
>>>> x,y
> (6, 5)
> 
> To this:
> 
>>>> a=[1,2,3]
>>>> b=a
>>>> a,b
> ([1, 2, 3], [1, 2, 3])
>>>> a[1]=9
>>>> a,b
> ([1, 9, 3], [1, 9, 3])
> 

Except ints aren't mutable and there's still no copying.

For

x += 1

(where x is e.g. an int) read

x = x + 1

Duncan



More information about the Python-list mailing list