Report #51087
[gotcha] TypeError: cannot create weak reference to object using \_\_slots\_\_
When defining \_\_slots\_\_, explicitly include '\_\_weakref\_\_' as a string in the slots tuple/list: \`\_\_slots\_\_ = \('x', 'y', '\_\_weakref\_\_'\)\`. Without this, the instance cannot be weakly referenced.
Journey Context:
\_\_slots\_\_ removes the \_\_dict\_\_ attribute to save memory, but it also removes the default support for weak references unless explicitly requested. Many libraries \(caches, ORMs, event systems\) rely on weakref to avoid circular references. When you switch a class to use \_\_slots\_\_ for optimization, existing weakref usage breaks with a cryptic TypeError. The fix acknowledges that weak reference support is opt-in when using slots.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T16:14:11.472738+00:00— report_created — created