[Tutor] When is = a copy and when is it an alias
spir
denis.spir at gmail.com
Mon Jan 27 10:53:08 CET 2014
On 01/27/2014 07:16 AM, Denis Heidtmann wrote:
> Running python 2.7 in linux
>
> Below are two extremes. Can I get some guidance on this?
>
> Thanks,
> -Denis H
>
>>>> a=zeros((2,3),dtype=int)
>>>> b=a
>>>> a[:,0]=[1,2]
>>>> a
> array([[1, 0, 0],
> [2, 0, 0]])
>>>> b
> array([[1, 0, 0],
> [2, 0, 0]])
>>>> a=2
>>>> a
> 2
>>>> b
> array([[1, 0, 0],
> [2, 0, 0]])
Note: your example is strongly obscured by using weird and rare features that
don't bring any helpful point to the actual problematic concepts you apparently
want to deal with.
It seems you are confusing 2 issues: relation (reference) between values
(objects) and relations between symbols (variables).
The last part of your example implies that you expect that, maybe, symbol 'b'
may now magically point to 2 just because it were corelated with 'a', which was
set to point to 2. Correct? If so, you are wrong: there is no relation between
symbols in python (nore in any other language I know, for the matter). Symbols
'a' and 'b' are independant, whatever the possible relations between their
values. If you *replace* a's value, this has no effect on b, even if they
previously held the very same, unique, value.
Python 3.3.2+ (default, Oct 9 2013, 14:50:09)
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = [1,2,3]
>>> b = a
>>> a is b
True
>>> a = (1,2) # replacement
>>> b
[1, 2, 3]
Now, there are 2 ways to change a symbol's value: to *replace* it globally as
above, or to *modify* it partly
>>> a = [1,2,3]
>>> b = a
>>> a is b
True
>>> a[1] = 0 # modification
>>> a
[1, 0, 3]
>>> b
[1, 0, 3]
>>> a is b
True
A misleading point is exemplified by "a is b": it does not mean that both
symbols actually are the same one (unique), but that _their value objects_ are
the same one (unique). This is the role of symbols: once defined, they are used
for whatever they represent. Here symbols a & b just play their normal role of
symbols, right?
The above example of modification is only possible if the value is complex (and
mutable). Simple values like numbers or strings obviously cannot be modified,
only replaced. Thus, such simple values just behave like "plain old values" in
static or historical languages (in which there aren't symbols & values are
runtime, instead plain adresses & raw data). In such languages there is
systematic copy on assignment, unless one explicitely uses pointers. Maybe you
are used to that, and expect such behaviour in python.
Quite the opposite, in python "symbolic assignment" (where the right side also
is a symbol) never copies, in fact never creates a new value, but bind the left
symbol to the same, unique value, as the right symbol.
Note: such are the semantics (the meaning) of the language. But since as said
above this does not make any difference in practice for simple values, the
language implementation is in fact free to copy under the hood, if this is
simpler or more efficient to do so: the language's semantics are preserved
nevertheless. However, python's standard implementation does not appear to do so:
>>> a = -12.345
>>> b = a
>>> a is b
True # a & b are bound to the same value, there's only one -12.345
d
More information about the Tutor
mailing list