#pylint: disable=too-few-public-methods #pylint: disable=missing-class-docstring #pylint: disable=missing-module-docstring import enum import weakref class TestEnum(enum.Enum): VAL1 = 'val1' VAL2 = 'val2' class Class1: def __init__(self): # Gracefully handle an exception of our own making try: raise ValueError() except ValueError: pass class Class2: def __init__(self): # Gracefully handle an exception of Enum's making try: TestEnum('invalid_value') except ValueError: pass # No strong refs here so these are free to die. class_1_ref = weakref.ref(Class1()) class_2_ref = weakref.ref(Class2()) # The exception raised by Enum creates a reference loop and thus # Class2 instances will stick around until the next gargage collection # cycle, unlike Class1. print(f'{class_1_ref=}\n{class_2_ref=}')