9.12. AliasingΒΆ

If a refers to an object and you assign b = a, then both variables refer to the same object:

Activity: CodeLens 9.12.1 (listAliasing)

The association of a variable with an object is called a reference. In this example, there are two references to the same object.

An object with more than one reference has more than one name, so we say that the object is aliased.

If the aliased object is mutable, changes made with one alias affect the other:

Activity: CodeLens 9.12.2 (listChange)

Although this behavior can be useful, it is error-prone. In general, it is safer to avoid aliasing when you are working with mutable objects.

For immutable objects like strings, aliasing is not as much of a problem. In this example:

a = 'banana'
b = 'banana'

it almost never makes a difference whether a and b refer to the same string or not.

You have attempted of activities on this page