Skip to content

Instantly share code, notes, and snippets.

@jasonpr59
Last active August 29, 2015 13:55
Show Gist options
  • Save jasonpr59/8726938 to your computer and use it in GitHub Desktop.
Save jasonpr59/8726938 to your computer and use it in GitHub Desktop.
An explanation of aliasing for Python. (The application to LA for 6.00 prompted me to explain a Python concept that I initially found challenging. This was my response.)

I'll explain the relationship between objects and names. That is, I'll explain aliasing.

In Python, we create objects and give them names. It's important to understand objects, names, and their relationship to each other-- at the very least it will help you avoid some bugs!

We create objects to represent data. For example, when we execute a = [4, 25], Python sees the [4, 25] and creates a list object.

When we create an object, we want to be able to refer back to it later. So we make a name that points to it. Above, we made the name a and pointed it at a list object. Then, when we use that name in a statement, Python finds the object that it points to so we can do things with the object-- like printing its value or modifying its data. For example, to execute a.append(400), Python looks at the name a, finds the object that it points to (the list), and then modifies the list by appending the value 400 to it.

We're allowed to have multiple names point to a single object. For example, we can make two names, a and b, and have both of them point to the same list object. Now, think about what happens if we call a.append(400). It modifies the list object-- the list object to which both a and b point. That is, it modifies the object that b points to, even though the statement doesn't use the name b! When we modify an object, it doesn't matter what name we used to get to it. That object is modified.

Not every "modification" that we make in Python is an object modification. It's possible to reassign names. For example, if next we execute, a = [100, 196], Python creates a new list object and makes a point to this new list, instead of the old list from the previous paragraphs. Now, if we modify the object to which a used to point, it has no effect on the new object to which a now points... they're totally different objects.

If you prefer more coding and less talking, take a look at this:

# Make a list object, and make the name "a" point to it.
a = [4, 25]
# Find the object that the name "a" points to, and
# make the name "b" point to it, too.
b = a
# Find the object that "b" points to, and modify it.
b.append(400)
# Find the object that "a" points to, and print it.
# This prints [4, 25, 400].
print a

# Next, make another list object, and make the name "a" point
# to it. That is, reassign "a" to point to the new object.
a = [100, 196]
# Find the object to which "b" points, (to which "a" *used*
# to point), and modify it.
b.append(500)
# Find the object to which "a" points, and print it. Notice
# that the previous statement did not modify it.
# This prints [100, 196].
print a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment