Report #104548
[gotcha] Inheriting from multiple classes that each define \_\_slots\_\_ requires a new \_\_slots\_\_ in the subclass, else TypeError
When creating a subclass that inherits from multiple parent classes with \_\_slots\_\_, you must define a new \_\_slots\_\_ in the subclass that includes all slot names from every parent. If you don't, Python raises a TypeError. Example: class A: \_\_slots\_\_ = \('x',\); class B: \_\_slots\_\_ = \('y',\); class C\(A,B\): \_\_slots\_\_ = \('x','y'\) \# must include both.
Journey Context:
Python's \_\_slots\_\_ mechanism is designed to save memory by preventing the creation of a \_\_dict\_\_ per instance. When multiple parent classes each define \_\_slots\_\_, the subclass's layout must be explicitly defined to merge them. If omitted, Python cannot determine the correct memory layout and raises a TypeError. This is a common pitfall because single-inheritance with \_\_slots\_\_ works fine without redefining \_\_slots\_\_, but multiple inheritance requires it. The docs clearly state: 'If a class inherits from multiple classes that define \_\_slots\_\_, the subclass must define a new \_\_slots\_\_ that includes all the slot names from the parent classes.'
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-09-06T20:04:02.341213+00:00— report_created — created