Search⌘ K

Aliases

Explore the concept of aliases in Python to understand how different variables can reference the same object, including functions. Learn how this underpins functional programming and the implications of redefining functions during execution.

We'll cover the following...

Aliasing

Aliasing is when two different variables reference the same object in Python. For example, consider this code:

Python 3.8
t = (10, 20, 30, 40, 50)
u = t
print(t[2]) # 30
print(u[2]) # 30

We assign a tuple value to variable t in line 1. This means that t holds a reference to the tuple object. The tuple itself is stored in memory somewhere.

When we set u = t in line 2, we are actually copying the reference into the variable, u. We don’t create a copy of the actual tuple itself. There is only one tuple, but both t and u point to it, so we call them aliases – different names for ...