September 2026
1051 pages
35h 4m
English
As you already know, in Python an assignment only creates a new reference to the same instance instead of creating a copy of the instance.
In the following example, s and t reference the same list, as the comparison with is reveals:
>>> s = [1, 2, 3]>>> t = s>>> t is sTrue
This approach doesn't always behave as intended because changes to the list referenced by s also affect t due to side effects and vice versa.
For example, if a method of a class returns a list that’s also used within the class, the list can also be modified via the returned reference, which is usually not what we want:
class MyClass: def __init__(self): self.list = [1, 2, 3] def get_list(self): return self.list def show_list(self): print(
Read now
Unlock full access