git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

I'm wrong or Will we fix the ducks limp?


On Wednesday, June 8, 2016 at 4:32:33 PM UTC+5:30, Antoon Pardon wrote:

> It means that if you mutate the object through one variable,
> you can see the result of that mutation through the other variable. But if the
> assignment doesn't mutate, you can't have such effect through assignment.
> 

Its called aliasing:
https://en.wikipedia.org/wiki/Aliasing_%28computing%29
And is the principal reason why we need to talk in terms of pointer/references/box-n-arrows
etc

> In python, you can sometimes simulate a mutating assignment and then we get this.
> 
>     >>> A = [8, 5, 3, 2]
>     >>> B = A
>     >>> B[:] = [3, 5, 8, 13]
>     >>> A
>     [3, 5, 8, 13]

Neat example -- thanks -- something for my tomorrow class
More telling than the one I usually use to talk of this stuff which runs thus:

>>> a=[[1,2,],[1,2]]
>>> a
[[1, 2], [1, 2]]
>>> inner=[1,2]
>>> b=[inner,inner]
>>> b
[[1, 2], [1, 2]]
# So a and b look the same


>>> a == b
True
# So even python thinks them the same

# But Uh... oh... 
>>> a[0][0]=3
>>> a
[[3, 2], [1, 2]]
>>> b[0][0]=3
>>> b
[[3, 2], [3, 2]]
# They dont behave the same!