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

Antoon Pardon antoon.pardon at vub.be
Wed Sep 27 02:56:03 EDT 2017


Op 27-09-17 om 04:58 schreef Steve D'Aprano:
> 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.

Yes it is!

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

You are hitting your blindspot again and ignore that in languages like Pascal ...
an assignment is a copy operation and thus mutates the variable that is assigned
to. Two variables are aliases for each other if they are the same object and so
when one mutates the object seen through one name, the mutation is visible through
the other.

Since Python in Python an assignent doesn't mutate the object but makes it an 
alias of an other object it is wrong to expect in python that an assignment
to one of an alias which would break the alias, would have the same effect
as in a language where an assignemt to one of an alias would mutate the variable. 

> 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,

No it is not. You forget the possibility that two names can be aliases at
one point but no longer are at an other point. 

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

That is only true for as long a and b remain aliases. As soon as an operation
is excuted that makes a and b no longer aliases, your test fails. Sure in
a language like C++ such an alias can't be broken, but aliases are broken
in Python all the time because that is what assignment do, break some
aliases and forge new ones.

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

No using a reference variable makes the two names aliases to the same object/entity.
So that if you mutate it through one name, the mutation is visible through the
other name. The effect you see in those languages with assignments via one
name are explained by the fact that assignment is a mutating operation.

> (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

In languages like C++, Pascal, ... aliases don't mean the names refer to
each other. Names/Identifiers refering to each other is non sensical in
those languages.

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

No, the model that C++ and Pascal use is not different in this aspect.

-- 
Antoon Pardon.





More information about the Python-list mailing list