At PyTennessee 2018 I gave a talk about writing Python object destructors called What To Expect When You’re Expiring: Rules for
__del__. The rules for a destructor method are:
- Don’t access modules or globals.
- Don’t access threadlocals.
- Don’t take any locks.
Links to further reading about the subject:
- PEP 442 makes Rule 1 less necessary. For an entertaining story about this rule, read A Normal Accident In Python and mod_wsgi.
- Python’s Thread Locals Are Weird. The horror story behind Rule 2: “Don’t touch threadlocals”.
- PyPy, Garbage Collection, And A Deadlock: More details about Rule 3.
- All the code examples used in my talk.
The images I used in my slides are from: