trey @ Python Morsels .com
© 2024 Akiyoshi Kitaoka, used with permission
#7c7c7c
βAll models are wrong,
but some models are useful.β— George E. P. Box
>>> first = []
>>> second = first
>>> second.append(9)
>>> second
[9]
>>> first
>>> first = []
>>> second = first
>>> second.append(9)
>>> second
[9]
>>> first
[9]
>>> first = []
>>> second = first
>>> second.append(9)
>>> second
[9]
>>> first
[9]
>>>
>>> first = []
>>> second = first
>>> second.append(9)
>>> second
[9]
>>> first
[9]
>>> rows = [second]
>>>
“... but they're not pointers”
“They're object references”
“They're name bindings”
binding | πͺ’ |
reference | π |
pointer | π |
“when I change first
, it doesn't always change second
”
>>> first = [2, 1, 3, 4]
>>> second = first
>>> first.append(7)
>>> second
[2, 1, 3, 4, 7]
>>> first = [100, 200, 300]
>>> second
[2, 1, 3, 4, 7]
>>>
>>> first = [2, 1, 3, 4]
>>> second = first
>>> first.append(7)
>>> second
[2, 1, 3, 4, 7]
>>> first = [100, 200, 300]
>>> second
[2, 1, 3, 4, 7]
>>>
Remember: variables point to objects
>>> first == second # Equality
True
>>> first is second # Identity
True
Identity: the exact same object
Equality: an equivalent object
>>> first == second # Are these objects equal to the other?
True
>>> first is second # Do these variables point to the same object?
True
value is None
value == another_value
>>> row = [0, 0, 0]
>>> boat = [row, row, row]
>>> boat
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> boat[1][1] = 1
>>> boat
[[0, 1, 0], [0, 1, 0], [0, 1, 0]]
>>> row
[0, 1, 0]
>>> first = []
>>> second = first
>>> second.append(9)
>>> second
[9]
>>> first
[9]
>>> rows = [second]
>>> lists = rows
>>> first = []
>>> second = first
>>> second.append(9)
>>> second
[9]
>>> first
[9]
>>> rows = [second]
>>>
>>> first = []
>>> second = first
>>> second.append(9)
>>> second
[9]
>>> first
[9]
>>> rows = [second]
>>>
>>> class TodoList:
... def __init__(self, tasks):
... self.tasks = tasks
...
... def add_task(self, task):
... self.tasks.append(task)
...
>>> default_todos = ["Reflect on last week"]
>>> mon = TodoList(default_todos)
>>> tue = TodoList(default_todos)
>>> mon.add_task("Work on talk")
>>> mon.tasks
['Reflect on last week', 'Work on talk']
>>> tue.tasks
['Reflect on last week', 'Work on talk']
>>> class TodoList:
... def __init__(self, tasks):
... #self.tasks = tasks
... self.tasks = list(tasks) # shallowly copy the list
... def add_task(self, task):
... self.tasks.append(task)
...
>>> default_todos = ["Reflect on last week"]
>>> mon = TodoList(default_todos)
>>> tue = TodoList(default_todos)
>>> mon.add_task("Work on talk")
>>> mon.tasks
['Reflect on last week', 'Work on talk']
>>> tue.tasks
['Reflect on last week']
>>> wed = TodoList({"Here is", "a set", "of tasks"})
x in y
loops over y
checking equality with x
>>> n = 3
>>> from math import pi
>>> for n in range(5):
... ...
...
>>> n
4
>>> def pi(): return 3.14159
...
>>> class pi: pass
...
+=
, -=
, *=
, etc.
Are these assignments?
Are these mutations?
>>> a = [2, 1, 3]
>>> b = a
>>> b += [4, 7, 11]
>>> b
[2, 1, 3, 4, 7, 11]
>>> a
[2, 1, 3, 4, 7, 11]
>>> name = "North Bay Python"
>>> name += " 2025" # name = name + "2025"
>>> name
'North Bay Python 2025'
>>> some_object.attribute = 4
>>> numbers[0] = 8
* For some definitions of "mutate"
>>> result = (True, [2, 1, 3])
>>> result[1].append(4)
>>> result
(True, [2, 1, 3, 4])
>>> result = (True, [2, 1, 3])
>>> result2 = (True, [2, 1, 3])
>>> result == result2
True
>>> result[1].append(4)
>>> result
(True, [2, 1, 3, 4])
>>> result == result2
False
>>> x = []
>>> x.append(x)
>>> x
[[...]]
>>> x in x
True
>>> x[0] is x
True
>>> x = []
>>> x.append(x)
>>> x
[[...]]
>>> x[0] is x
True
>>> x in x
True
Names, Objects, and Plummeting From The Cliff - Brandon Rhodes
Facts and Myths about Names and Values - Ned Batchelder
Would every assignment statement make a copy?
Would storing a reference to an object ever be possible?
"reference assignments" and "copying assignments" π¬
Trey Hunner
Python Team Trainer
[email protected]
>>> rows = [[0] * 3] * 3
>>> rows
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> rows[1][1] = 1
>>> rows
[[0, 1, 0], [0, 1, 0], [0, 1, 0]]
>>> result = (True, [2, 1, 3, 4])
>>> result[1] += [7, 11, 18]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
result[1] += [7, 11, 18]
~~~~~~^^^
TypeError: 'tuple' object does not support item assignment
>>> result
(True, [2, 1, 3, 4, 7, 11, 18])