June 2019
Beginner to intermediate
770 pages
19h 24m
English
The following is a common situation that involves circularity. One class, Parent, contains a collection of children. Each Child instance contains a reference to the Parent class. We'll use these two classes to examine circular references:
class Parent: def __init__(self, *children: 'Child') -> None: for child in children: child.parent = self self.children = {c.id: c for c in children} def __del__(self) -> None: print( f"Removing {self.__class__.__name__} {id(self):d}" )class Child: def __init__(self, id: str) -> None: self.id = id self.parent: Parent = cast(Parent, None) def __del__(self) -> None: print( f"Removing {self.__class__.__name__} {id(self):d}" )
A Parent instance has a collection of children ...
Read now
Unlock full access