Under The Hood
backend python CPython 3.12+

Descriptors: __get__, __set__, @property, and __slots__ Memory Savings

Last updated
Prerequisites:
Python Classes and Attributes
Python Data Model
  • python
  • descriptors
  • property
  • slots
  • memory-optimization
  • attribute-lookup

Read at your depth

The practical view

A Descriptor is an object attribute with binding behavior, defined by implementing `__get__`, `__set__`, or `__delete__`. `@property` is the most common built-in descriptor. Adding `__slots__ = ('x', 'y')` to a class tells Python not to create an instance `__dict__` or `__weakref__`, storing attributes in a compact C-level array and reducing per-instance memory consumption by 60-70%.

The same idea in other frameworks

python equivalent
`__slots__ = ('name', 'age')` and Descriptor protocol (`__get__`/`__set__`).

Legacy vs modern

Memory-Heavy Instance __dict__ vs Compact __slots__

1,000,000 standard instances consume hundreds of megabytes of `__dict__` memory; `__slots__` cuts RAM usage by ~70%.

before → after
Standard Class with __dict__
class Point: def __init__(self, x, y): self.x = x; self.y = y // ❌ 150 bytes per instance
Memory-Optimized __slots__
__slots__ = ('x', 'y') // ✅ NEW: Eliminates __dict__, saves 70% RAM

Interview gotchas

Context

Interviewers test your knowledge of inheritance rules with `__slots__`.

The mechanical answer

If a subclass does not define its own `__slots__`, **it will create an instance `__dict__` anyway**, completely defeating the memory optimization for instances of the subclass. To preserve memory savings down an inheritance chain, **every subclass must explicitly declare `__slots__ = ()`** (or specify its additional slot attributes).

Trap

Inheriting from a class with `__slots__` without declaring `__slots__` in the child silently restores `__dict__` allocation.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.