liveBook · Manning

Alias

As explained in Get Programming: Learn to code with Python

An alias is an alternative name by which an entity, command, parameter, or resource can be referred to.

An alias is another name for the same object. In Python, assigning one variable to another does not copy the object. This matters most with lists and dictionaries: a change made through one name is visible through every other name. section 28.3

Think of a person known by both a full name and a nickname. The names differ, but they identify the same person. Likewise, two variables can point to one object rather than hold separate objects. section 28.0

Two names, one list

genius = ["einstein", "galileo"] smart = genius genius.append("shakespeare") First, genius points to a list. Next, smart becomes another name for that list—not a copy. Finally, append changes the list itself. Both genius and smart now show ["einstein", "galileo", "shakespeare"]. Calling id() on each name shows the same object ID. section 28.1

Figure 28.2. When the list object ["einstein", "galileo"] is modified through the variable genius, the variable smart also points to the modified list object.

The diagram shows both variable names pointing to the list after it has been changed through one name. section 28.1

Aliasing also applies to immutable objects. If a and b both point to the integer 1, then a = 2 makes a point to a different object; b still points to 1. Reassignment changes a name’s destination. It does not change the object another name points to. section 28.1

When you need a separate list

artists = ["monet", "picasso"] painters = artists.copy() painters.append("van gogh") Here, painters is a copy rather than an alias. Adding an item to painters leaves artists unchanged. A quick check: if the second line were painters = artists, would artists change after the append? Yes—both names would point to the same list. section 28.2 section 28.3

Remember the distinction when passing a dictionary to a function that changes it: changes to the shared object remain visible afterward. Use an alias when shared changes are intended; make a copy when the original must stay unchanged. section 28.1 section 28.2

How Get Programming: Learn to code with Python covers it

How deep it goes
Works through object IDs, reassignment, and shared list mutation, then contrasts aliasing with copying without detailing memory implementation.
Examples
Uses nicknames, shared list names, object IDs, and a copied artists list to make object identity visible.
What it assumes
Assumes basic Python variables and assignment while introducing aliases through small list examples and id() checks.
What you can do after
Diagnose/debug with it when a list or dictionary changes unexpectedly through another variable, and choose a copy when independence is needed.
How it connects
Connects aliases to variable assignment, mutable and immutable objects, list and dictionary mutation, and copying.

Also explained in

Compare how 6 books explain alias →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.