Aliasing [was Re: [Tutor] beginning to code]

Steve D'Aprano steve+python at pearwood.info
Tue Sep 26 22:58:54 EDT 2017


On Wed, 27 Sep 2017 02:03 am, Stefan Ram wrote:

> Steve D'Aprano <steve+python at pearwood.info> writes:
>>On Tue, 26 Sep 2017 03:26 am, Antoon Pardon wrote:
>>>at that moment, but it still needed correction. If the assignment is
>>>an alias operator then after the statements
>>Here's some C++ code that demonstrates it. Apologies in advance if it isn't
>>the most idiomatic C++ code.
> 
>   In C++, assignments and initializations are different
>   concepts.
> 
>>int& b = a;  // reference variable or alias
> 
>   This is an initialization, not an assignment.

A pedantic difference that makes no difference to my argument.

I see that you ignored the later assignment:

b = 2;

which also assigned to a. *That** is the fundamental point: b is certainly an
alias for a, and assigning to b assigns to a.

That's how aliases work in C++. That's how var parameters in Pascal work, and
out parameters in Ada. That is what it means to say that "b is an alias to a".

b is another name for the *variable* a, not just whatever value a happens to
hold now.

I say that assignment in Python is NOT an aliasing operation. Antoon claims I'm
wrong, and his evidence is:

a = []
b = a  # Antoon says this is an alias operation
b.append(1)
assert a == [1]


But that's not enough for the variable b to be an alias for the variable a.

Antoon is correct that a and b are two different names for the same list, but
the two variables are not aliases to each other because assignments to b do not
affect a, and vice versa.

A good test for aliasing is to take the source code and mechanically replace
every occurrence of the alias (in the same scope of course) with the original,
or vice versa, and see whether the meaning of the code changes.

In C++, apart from the initial binding:

    int& b = a;

("initialisation") you could now randomly swap a for b or b for a and the
meaning of the code will not change.

But in Python, if we try the same trick, the code *does* change:

a = 1
b = a
b = 2

*is not* the same as:

a = 1
b = a
a = 2


(1) In Pascal, Ada, C++ etc using a reference variable (or var parameter, or
whatever terminology they use) makes the two names aliases to EACH OTHER, not
to the value they are bound to.

(2) In Python, Javascript, Ruby etc assignment can give you two names for the
same object, but the names do not alias each other.

The critical distinction here is whether the names refer to each other:

a <---> b

or whether they merely refer to the same value:

a ---> [ value ] <--- b


Python uses the second model. Var parameters in Pascal and references in C++ use
the first. Since the term "aliasing" is well-established for the first, using
it in Python *without making the difference clear* is wrong.


-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list