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/
Domain: thecodeforge.io
{
"@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: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