← Introduction to Computer Science

Unit 10: References, Aliasing, and Mutability

What a variable is actually holding, two names pointing at one object, and why == and is don't always agree.

5 topics · 16 questions

Each topic is a path, not a menu: work down it in order and every question builds on the one above. Click any of them to watch the answer draw itself.

Variables as References to Objects

  1. How it worksHow does a variable store a reference to an object in memory instead of the object's data directly?
  2. Worked exampleTrace a = [1, 2, 3] then b = a and find what changes in a after b.append(4).
  3. Common mistakeWhy isn't b = a the same as building a brand new, separate list?
Your turn1 of 2
a = [1, 2, 3] then b = a. b.append(70) is called 7 times. How many elements does a have now, since a and b reference the same list?

Aliasing: Two Names, One Object

  1. How it worksHow do two variable names become aliases for the exact same object sitting in memory?
  2. Worked exampleTrace cart = [] then backup = cart then cart.append('item') and check what backup holds.
  3. Going furtherWhat happens once backup is reassigned to a brand new empty list instead of mutated?
Your turn1 of 2
cart = [] then backup = cart, aliasing the same list. cart.append('item') is called 35 times. How many items does backup show, since it's the same object?

Mutable Versus Immutable Types

  1. How it worksHow does an immutable type force every change to build and return a new object instead of editing in place?
  2. Worked exampleTrace t = (1, 2) attempting t[0] = 9 versus lst = [1, 2] running lst[0] = 9.
  3. Common mistakeWhy doesn't reassigning a variable holding an immutable value count as mutating that value?
Your turn1 of 2
A list starts with 35 elements. Its 0th element is reassigned 7 times, which is allowed since lists are mutable. How many elements does the list have after these k reassignments?

Shallow Copy Versus Deep Copy

  1. How it worksHow does a shallow copy duplicate the outer list but leave every inner list shared between both copies?
  2. Worked exampleTrace outer = [[1, 2], [3, 4]] shallow-copied, then outer[0].append(9), and check the copy.
  3. Going furtherWhat changes once a deep copy is used instead of a shallow one on the same nested list?
Your turn1 of 2
A nested list outer holds 15 inner lists, each shared by reference in a shallow copy. If one inner list is appended to 7 times through the original, how many times does the shallow copy's corresponding inner list reflect those same appends?

Equality Versus Identity: == and is

  1. How it worksHow does == compare contents while is compares whether two names point to one single object?
  2. Worked exampleTrace a = [1, 2] and b = [1, 2] and check a == b against a is b.
  3. Common mistakeWhy doesn't == always mean the same thing as checking whether two variables share one identity?
  4. Ties togetherHow does the difference between == and is connect back to aliasing two names to one object?
Your turn1 of 2
Two separate list objects a and b each hold 21 elements with equal, matching contents, so a == b evaluates to true. A third variable c is created as c = a, making c and a reference the exact same object. If b.append(33) is called, changing only b's contents, how many elements does the list referenced by c have?