Title: Python __slots__ — Why Your Subclass Still Has __dict__
Open Graph Title: Python __slots__ — Why Your Subclass Still Has __dict__
Description: MemoryError processing 3.2M points: expected 220MB, got 1.2GB. Subclasses need their own __slots__ to prevent __dict__ overhead. Save gigabytes.
Open Graph Description: MemoryError processing 3.2M points: expected 220MB, got 1.2GB. Subclasses need their own __slots__ to prevent __dict__ overhead. Save gigabytes.
Opengraph URL: https://thecodeforge.io/python/python-slots/
Domain: thecodeforge.io
{
"@context": "https://schema.org",
"@type": "TechArticle",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://thecodeforge.io/python/python-slots/"
},
"headline": "Python __slots__ — Why Your Subclass Still Has __dict__",
"description": "MemoryError processing 3.2M points: expected 220MB, got 1.2GB. Subclasses need their own __slots__ to prevent __dict__ overhead. Save gigabytes.",
"keywords": "Python __slots__ — Why Your Subclass Still Has __dict__, Python, Advanced, Advanced Python, MemoryError processing 3.2M points: expected 220MB, got 1.2GB. Subclasses need their own __slots__ to prevent __dict__ overhead. Save gigabytes.",
"proficiencyLevel": "Expert",
"author": {
"@type": "Person",
"@id": "https://thecodeforge.io/about/#naren",
"name": "Naren",
"url": "https://thecodeforge.io/about/"
},
"publisher": {
"@type": "Organization",
"@id": "https://thecodeforge.io/#org",
"name": "TheCodeForge",
"url": "https://thecodeforge.io",
"logo": {"@type": "ImageObject", "url": "https://thecodeforge.io/assets/og-image.png"}
},
"datePublished": "2026-03-17T00:00:00+00:00",
"dateModified": "2026-10-31T00:00:00+00:00",
"url": "https://thecodeforge.io/python/python-slots/",
"inLanguage": "en",
"educationalLevel": "Advanced",
"image": "https://thecodeforge.io/assets/og-image.png",
"isPartOf": {"@id": "https://thecodeforge.io/#website"}
}
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{"@type": "ListItem", "position": 1, "name": "Home", "item": "https://thecodeforge.io/"},
{"@type": "ListItem", "position": 2, "name": "Python", "item": "https://thecodeforge.io/python/"},
{"@type": "ListItem", "position": 3, "name": "Python __slots__ — Why Your Subclass Still Has __dict__", "item": "https://thecodeforge.io/python/python-slots/"}
]
}
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "Does __slots__ work with inheritance?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Only if all classes in the hierarchy define __slots__. If the parent class does not define __slots__ (or defines it without including __dict__), instances will still have a __dict__. Each class in the hierarchy should define only the new slots it introduces, not repeat the parent's slots."
}
},
{
"@type": "Question",
"name": "Can I use __slots__ with dataclasses?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Yes. Python 3.10+ added slots=True to @dataclass: @dataclass(slots=True). This automatically generates __slots__ from the field definitions, giving you the memory benefits without manually writing __slots__."
}
},
{
"@type": "Question",
"name": "Does __slots__ speed up attribute access?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Yes, by about 10-20% in microbenchmarks. But in most applications the overall improvement is under 3% because attribute access is rarely the bottleneck. The memory savings are the primary benefit."
}
},
{
"@type": "Question",
"name": "Can I have both __slots__ and dynamic attribute assignment?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Yes, by including '__dict__' in the __slots__ tuple: __slots__ = ('x', '__dict__'). This keeps __dict__ but adds the C-level slot descriptors for named attributes. However, you lose most of the memory benefit (about 80% of the savings) because the dict still exists."
}
},
{
"@type": "Question",
"name": "Why can't I use __slots__ with Django models?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Django's ORM relies on __dict__ to inject dynamic attributes (like related objects, field tracking, and instance states). If a model class uses __slots__, Django can't set these attributes, causing AttributeError. You can include '__dict__' in __slots__ but then you lose the memory benefit."
}
}
]
}
| og:type | website |
| og:image | https://thecodeforge.io/assets/og-image.png |
| og:image:width | 1200 |
| og:image:height | 630 |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: max-image-preview:large