Variables and Memory · Practice
Quiz & exercises
Answer each question to see the explanation. Your best score is saved in this browser.
Quiz
Coding exercises
Write your code, then press Check (or Shift+Enter) to run the tests. Drafts are saved as you type.
Exercise 1: Same object or just equal? ✓ Passed
Write a function relationship(a, b) that returns:
'same object' if a and b refer to the same object, otherwise 'equal' if their values are equal, otherwise 'different'.
def relationship(a, b):
# your code here
pass
def test_same():
"""same object"""
x = [1, 2]
assert relationship(x, x) == 'same object', f"got {relationship(x, x)!r}"
def test_equal():
"""equal but distinct lists"""
assert relationship([1, 2], [1, 2]) == 'equal'
def test_cross_type():
"""10 and 10.0 are equal, not the same object"""
assert relationship(10, 10.0) == 'equal'
def test_different():
"""different values"""
assert relationship('a', 'b') == 'different'
Hints (2)
- Check identity first with
is. Two names for the same object are always equal too. - Order matters: if you test
==first, you can never return'same object'.
Show a solution
def relationship(a, b):
if a is b:
return 'same object'
if a == b:
return 'equal'
return 'different'Exercise 2: A function without side effects ✓ Passed
add_item(items, item) is supposed to return a new list with item appended, but the current version also changes the caller's list (a side effect).
Fix it so the original list is never mutated and the returned list is a different object.
def add_item(items, item):
items.append(item)
return items
def test_returns_new_list():
"""returns the list with the item added"""
assert add_item([1, 2], 3) == [1, 2, 3]
def test_original_unchanged():
"""caller's list is not mutated"""
original = [1, 2]
add_item(original, 3)
assert original == [1, 2], f"original was changed to {original}"
def test_new_object():
"""result is a new list object"""
original = [1, 2]
result = add_item(original, 3)
assert result is not original, "returned the same list object"
Hints (2)
items.append(...)mutates the shared list. Which list operation creates a new list?- Alternatively, copy first with
list(items)oritems.copy(), then append to the copy.
Show a solution
def add_item(items, item):
return items + [item]Exercise 3: Count the references ✓ Passed
Using the course's ctypes technique, write ref_count(obj) that returns how many references point to obj, not counting the temporary reference created by calling your function.
Tip: read the count from id(obj). Then think about how many extra references exist while your function is running.
import ctypes
def ref_count(obj):
# your code here
pass
def test_single_reference():
"""a list held by one variable has 1 reference"""
data = [1, 2, 3]
assert ref_count(data) == 1, f"got {ref_count(data)}"
def test_two_references():
"""adding an alias increases the count to 2"""
data = [1, 2, 3]
alias = data
assert ref_count(data) == 2, f"got {ref_count(data)}"
def test_in_container():
"""being stored in a list counts as a reference"""
data = {}
holder = [data, data]
assert ref_count(data) == 3, f"got {ref_count(data)}"
Hints (2)
ctypes.c_long.from_address(id(obj)).valuereads the raw count.- Your function's own parameter
objis one extra reference while the function is running. Subtract it.
Show a solution
import ctypes
def ref_count(obj):
# While this function runs, its parameter `obj` holds one extra reference.
return ctypes.c_long.from_address(id(obj)).value - 1