René's URL Explorer Experiment


Title: Python Descriptors — Missing Instance Dict Shared State Bug

Open Graph Title: Python Descriptors — Missing Instance Dict Shared State Bug

Description: Two DB servers identical counts despite 300% workload difference — store data on instance __dict__, not descriptor. Fix: key prefixed with underscore.

Open Graph Description: Two DB servers identical counts despite 300% workload difference — store data on instance __dict__, not descriptor. Fix: key prefixed with underscore.

Opengraph URL: https://thecodeforge.io/python/python-descriptors/

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-descriptors/"
  },
  "headline": "Python Descriptors — Missing Instance Dict Shared State Bug",
  "description": "Two DB servers identical counts despite 300% workload difference — store data on instance __dict__, not descriptor. Fix: key prefixed with underscore.",
  "keywords": "Python Descriptors — Missing Instance Dict Shared State Bug, Python, Advanced, Advanced Python, Two DB servers identical counts despite 300% workload difference — store data on instance __dict__, not descriptor. Fix: key prefixed with underscore.",
  "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-05T00:00:00+00:00",
  "dateModified": "2026-09-26T00:00:00+00:00",
  "url": "https://thecodeforge.io/python/python-descriptors/",
  "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 Descriptors — Missing Instance Dict Shared State Bug", "item": "https://thecodeforge.io/python/python-descriptors/"}
  ]
}
{
  "@context": "https://schema.org",
  "@type": "FAQPage",
  "mainEntity": [
    {
      "@type": "Question",
      "name": "Can I use a descriptor on a class level (not on instances)?",
      "acceptedAnswer": {
        "@type": "Answer",
        "text": "Yes. When you access a descriptor on the class itself (e.g., `MyClass.attr`), Python calls `__get__` with `instance=None` and `owner=MyClass`. You must handle this in your `__get__` implementation — typically by returning `self` (the descriptor object) or a class-level value. If you don't, you'll get an `AttributeError` when trying to access `instance.__dict__` on `None`."
      }
    },
    {
      "@type": "Question",
      "name": "What's the difference between a descriptor and a property?",
      "acceptedAnswer": {
        "@type": "Answer",
        "text": "A property is a specific type of descriptor — it's a built-in class that implements the full descriptor protocol (`__get__`, `__set__`, `__delete__`). You can write your own descriptors to do anything a property does and more: validation, type checking, lazy loading, logging, and cross-attribute coordination. Properties are syntactic sugar for simple getter/setter patterns; descriptors are a general-purpose hook into the attribute system."
      }
    },
    {
      "@type": "Question",
      "name": "Why does cached_property not work with __slots__?",
      "acceptedAnswer": {
        "@type": "Answer",
        "text": "`cached_property` works by writing the cached value directly into `instance.__dict__` under the attribute name. If a class defines `__slots__`, instances don't have a `__dict__` unless explicitly included (`__slots__ = ('__dict__',)`). Without `__dict__`, `cached_property` has nowhere to store the computed value and raises a `TypeError`. Workaround: either add `'__dict__'` to `__slots__`, drop `__slots__`, or implement a custom caching descriptor that stores the value in a designated slot."
      }
    },
    {
      "@type": "Question",
      "name": "Can a descriptor be used on a function or method?",
      "acceptedAnswer": {
        "@type": "Answer",
        "text": "No — descriptors are defined on classes, not on functions. The `__get__` method of a function is what makes it a descriptor (it returns a bound method when accessed on an instance). But you cannot assign a descriptor object as an attribute of a function; it must be a class attribute. If you want to intercept attribute access on a module-level object, consider using `__getattr__` on the module or a proxy class."
      }
    },
    {
      "@type": "Question",
      "name": "How do I debug why a descriptor isn't being called?",
      "acceptedAnswer": {
        "@type": "Answer",
        "text": "First, check if the attribute is defined on the class or on the instance. Print `type(instance).__dict__.get('attr')` — if it returns a descriptor object, it's a class attribute. Then check if it's a data or non-data descriptor (presence of `__set__` or `__delete__`). If it's a non-data descriptor and the instance has the same key in `__dict__`, the instance value wins. Temporarily add a `print()` inside `__get__` to confirm it's being invoked. Also verify that `__set_name__` was called (the storage key should not be None)."
      }
    }
  ]
}

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-descriptors/#v2-production-incident
 🔎 Debug Guidehttps://thecodeforge.io/python/python-descriptors/#v2-debug-guide
 ⚙ Triage Commandshttps://thecodeforge.io/python/python-descriptors/#v2-triage-commands
Pythonhttps://thecodeforge.io/python/introduction-python/
Djangohttps://thecodeforge.io/interview/django-interview-questions/
SQLAlchemyhttps://thecodeforge.io/python/sqlalchemy-basics/
NumPyhttps://thecodeforge.io/python/numpy-basics/
arrayhttps://thecodeforge.io/dsa/array-data-structure/
pytest fixtureshttps://thecodeforge.io/python/pytest-fixtures/
decoratorhttps://thecodeforge.io/java/decorator-pattern-java/
→Python __slots__ — Why Your Subclass Still Has __dict__MemoryError processing 3.2M points: expected 220MB, got 1.2GB. Subclasses need their own __slots__ to prevent __dict__ ohttps://thecodeforge.io/python/python-slots/
→Python __init__ Mutable Defaults — The Shared State BugDefault list in __init__ caused cross-user data leaks in production. How Python evaluates mutable defaults once at definhttps://thecodeforge.io/python/classes-objects-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/
→Python Dataclasses — Mutable Default Traps That Break ProdShared list defaults corrupted customer orders across instances. Learn why @dataclass protection fails when you overridehttps://thecodeforge.io/python/dataclasses-python/
Model Deployment with FlaskAdvancedhttps://thecodeforge.io/ml-ai/model-deployment-flask/
Follow https://www.linkedin.com/in/thecodeforge-io-a0676b3b5/
←PreviousMemory Management in Pythonhttps://thecodeforge.io/python/memory-management-python/
NextType Hints in Python→https://thecodeforge.io/python/type-hints-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.