René's URL Explorer Experiment


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/

direct link

Domain: thecodeforge.io


Hey, it has json ld scripts:
{
  "@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:typewebsite
og:imagehttps://thecodeforge.io/assets/og-image.png
og:image:width1200
og:image:height630

Links:

TheCodeForge https://thecodeforge.io/
Pythonhttps://thecodeforge.io/python/
Buy me a coffee https://ko-fi.com/thecodeforge
1Iterators and Iterables in Pythonhttps://thecodeforge.io/python/iterators-iterables-python/
2Coroutines and asyncio in Pythonhttps://thecodeforge.io/python/coroutines-asyncio-python/
3Metaclasses in Pythonhttps://thecodeforge.io/python/metaclasses-python/
4Memory Management in Pythonhttps://thecodeforge.io/python/memory-management-python/
5Python Descriptorshttps://thecodeforge.io/python/python-descriptors/
6Type Hints in Pythonhttps://thecodeforge.io/python/type-hints-python/
7Unit Testing with pytesthttps://thecodeforge.io/python/unit-testing-pytest/
8Virtual Environments in Pythonhttps://thecodeforge.io/python/virtual-environments-python/
9Python Packaging and piphttps://thecodeforge.io/python/python-packaging-pip/
10GIL — Global Interpreter Lockhttps://thecodeforge.io/python/global-interpreter-lock-python/
11Python Slotshttps://thecodeforge.io/python/python-slots/
12Abstract Base Classes in Pythonhttps://thecodeforge.io/python/abstract-base-classes-python/
13Python Design Patternshttps://thecodeforge.io/python/python-design-patterns/
14Python Performance Optimisationhttps://thecodeforge.io/python/python-performance-optimisation/
15Python Concurrency — asyncio Deep Divehttps://thecodeforge.io/python/python-asyncio-deep-dive/
16Python Weak Referenceshttps://thecodeforge.io/python/python-weak-references/
17The Zen of Python: 19 Principles That Explain Every Design Decisionhttps://thecodeforge.io/python/zen-of-python/
← Back to Python track https://thecodeforge.io/python/
Homehttps://thecodeforge.io/
Pythonhttps://thecodeforge.io/python/
Follow https://www.linkedin.com/in/thecodeforge-io-a0676b3b5/
 ● Production Incidenthttps://thecodeforge.io/python/python-slots/#v2-production-incident
 🔎 Debug Guidehttps://thecodeforge.io/python/python-slots/#v2-debug-guide
 ⚙ Triage Commandshttps://thecodeforge.io/python/python-slots/#v2-triage-commands
Pythonhttps://thecodeforge.io/python/introduction-python/
recordshttps://thecodeforge.io/java/records-java16/
arrayhttps://thecodeforge.io/dsa/array-data-structure/
inheritancehttps://thecodeforge.io/python/inheritance-python/
transferhttps://thecodeforge.io/ml-ai/transfer-learning/
Djangohttps://thecodeforge.io/interview/django-interview-questions/
SQLAlchemyhttps://thecodeforge.io/python/sqlalchemy-basics/
→Python Performance Optimisation — O(n*m) Loop Cost87% of runtime was a linear membership check on 200K entries. That O(n*m) pattern was costly. Profile first to avoid thihttps://thecodeforge.io/python/python-performance-optimisation/
→Python Descriptors — Missing Instance Dict Shared State BugTwo DB servers identical counts despite 300% workload difference — store data on instance __dict__, not descriptor. Fix:https://thecodeforge.io/python/python-descriptors/
→Python Memory Management — Event Bus That Ate 8GBRSS grew ~200MB/day from never-removed event bus handlers. Fix it with weakref and tracemalloc to prevent 8GB leaks — thhttps://thecodeforge.io/python/memory-management-python/
→Python Magic Methods — Set Corruption When __hash__ MissingOrders missing from set when __eq__ defined without __hash__ triggers TypeError and silent corruption. Use frozen dataclhttps://thecodeforge.io/python/magic-methods-python/
→Encapsulation in Python Explained — Access Control, Properties and Real-World PatternsEncapsulation in Python demystified: learn why access control matters, how name mangling works, when to use properties,https://thecodeforge.io/python/encapsulation-python/
Keras Callbacks — ModelCheckpoint and EarlyStoppingIntermediatehttps://thecodeforge.io/ml-ai/keras-callbacks/
Follow https://www.linkedin.com/in/thecodeforge-io-a0676b3b5/
←PreviousGIL — Global Interpreter Lockhttps://thecodeforge.io/python/global-interpreter-lock-python/
NextAbstract Base Classes in Python→https://thecodeforge.io/python/abstract-base-classes-python/
LinkedIn https://www.linkedin.com/in/thecodeforge-io-a0676b3b5/
Facebook https://www.facebook.com/profile.php?id=61583020033815
YouTube https://www.youtube.com/channel/UCMar7CF2EmCF1I2tw1tNVuQ
Javahttps://thecodeforge.io/java/
Pythonhttps://thecodeforge.io/python/
C / C++https://thecodeforge.io/c-cpp/
JavaScripthttps://thecodeforge.io/javascript/
DSAhttps://thecodeforge.io/dsa/
Databasehttps://thecodeforge.io/database/
System Designhttps://thecodeforge.io/system-design/
DevOpshttps://thecodeforge.io/devops/
C# / .NEThttps://thecodeforge.io/csharp/
PHPhttps://thecodeforge.io/php/
ML / AIhttps://thecodeforge.io/ml-ai/
Interviewhttps://thecodeforge.io/interview/
CS Fundamentalshttps://thecodeforge.io/cs-fundamentals/
Rusthttps://thecodeforge.io/rust/
Toolshttps://thecodeforge.io/tools/
Cheatsheetshttps://thecodeforge.io/cheatsheets/
Mobilehttps://thecodeforge.io/mobile/
Data Analyticshttps://thecodeforge.io/data-analytics/
Securityhttps://thecodeforge.io/security/
Frontendhttps://thecodeforge.io/frontend/
Data Engineeringhttps://thecodeforge.io/data-engineering/
Testinghttps://thecodeforge.io/testing/
Cloudhttps://thecodeforge.io/cloud/
Observabilityhttps://thecodeforge.io/observability/
Web Platformhttps://thecodeforge.io/web-platform/
🧯 Errors Explainedhttps://thecodeforge.io/errors/
⚒ Blueprintshttps://thecodeforge.io/blueprints/
⌨ Practicehttps://thecodeforge.io/practice/
Interview Prephttps://thecodeforge.io/interview/
DSA Problemshttps://thecodeforge.io/dsa/
Developer Toolshttps://thecodeforge.io/tools/
Cheat Sheetshttps://thecodeforge.io/cheatsheets/
Abouthttps://thecodeforge.io/about/
Sitemaphttps://thecodeforge.io/sitemap.xml
Privacyhttps://thecodeforge.io/privacy.html

Viewport: width=device-width, initial-scale=1.0

Robots: max-image-preview:large


URLs of crawlers that visited me.