June 2019
Beginner to intermediate
770 pages
19h 24m
English
For CPython implementation, objects have a reference count. The count is incremented when the object is assigned to a variable and decremented when the variable is removed. When the reference count is zero, the object is no longer needed and can be destroyed. For simple objects, __del__() will be invoked and the object will be removed.
For complex objects that have circular references among objects, the reference count might never go to zero and __del__() can't be invoked easily. The following is a class that we can use to see what happens:
class Noisy: def __del__(self) -> None: print(f"Removing {id(self)}")
We can create (and see the removal of) these objects as follows:
>>> x = Noisy() >>> del x Removing ...
Read now
Unlock full access