June 2019
Beginner to intermediate
770 pages
19h 24m
English
In cases where we need circular references but also want __del__() to work nicely, we can use weak references. One common use case for circular references is mutual references: a parent with a collection of children where each child has a reference back to the parent. If a Player class has multiple hands, it might be helpful for a Hand object to contain a weak reference to the owning Player class.
The default object references could be called strong references; however, direct references is a better term. They're used by the reference-counting mechanism in Python; they cannot be ignored.
Consider the following statement:
a = B()
The a variable has a direct reference to the object of the B class ...
Read now
Unlock full access