References
When we assign variables such as theSum = 0
, we are assigning references.
This is especially important to note when we are copying lists for example.
The above produces:
This is because we have essentially copied the reference of myList
three times into A
. Mutating myList
will then also mutate A
.
If we have two objects A
and B
. A == B
will only be true if the reference of A
is the same as B
even though the contents of A
may be exactly the same as B
. This is known as shallow equality.
Deep equality can be achieved by overriding the __eq__
method in the class (see OOP section below).