# Descriptors Protocol in Depth

> How does the Python descriptor protocol work? What is the difference between data and non-data descriptors?

Canonical: https://www.internhack.in/learn/interview/python-interview/python-descriptors
Last updated: 2026-11-28

Difficulty: Advanced

## Question

How does the Python descriptor protocol work? What is the difference between data and non-data descriptors?

## Answer

A descriptor is an object that implements at least one of: \__get_\_, \__set_\_, or \__delete_\_.

Data descriptor: implements \__set_\_ (and usually \__get_\_). Takes priority over instance \__dict_\_. Examples: property, classmethod, staticmethod.

Non-data descriptor: implements only \__get_\_. Instance \__dict_\_ takes priority over it. Examples: functions (that is how method binding works).

Lookup order for obj.attr:

1. type(obj).\__mro_\_ data descriptors
2. obj.\__dict_\_
3. type(obj).\__mro_\_ non-data descriptors

This is how functions become bound methods: they are non-data descriptors with a \__get_\_ that returns a bound method.

## Code examples

### Functions are Non-Data Descriptors

```
# This explains how methods work
class MyClass:
    def greet(self):
        return f'Hello from {self}'

obj = MyClass()

# greet is a function object (descriptor)
print(type(MyClass.__dict__['greet']))  # <class 'function'>

# Accessing via instance triggers __get__ -> bound method
print(type(obj.greet))   # <class 'method'>
print(obj.greet())        # Hello from <MyClass object>

# Manually: function.__get__(instance, class) returns bound method
bm = MyClass.__dict__['greet'].__get__(obj, MyClass)
print(bm())   # Same as obj.greet()

# Non-data descriptor: instance dict takes priority
class NonData:
    def __get__(self, obj, objtype=None):
        if obj is None: return self
        return 'from descriptor'

class Test:
    attr = NonData()

t = Test()
print(t.attr)           # from descriptor
t.__dict__['attr'] = 'from instance'  # Override!
print(t.attr)           # from instance (instance dict wins)
```

Functions implement \__get_\_ to return bound methods - this is the descriptor protocol in action. Non-data descriptors (only \__get_\_) can be shadowed by instance dict.

### Data Descriptor: Lazy Property

```
class cached_property:
    """A data descriptor that computes and caches a property once."""

    def __init__(self, func):
        self.func = func
        self.attr = f'_cache_{func.__name__}'

    def __set_name__(self, owner, name):
        self.attr = f'_cache_{name}'

    def __get__(self, obj, objtype=None):
        if obj is None:
            return self
        if not hasattr(obj, self.attr):
            print(f'Computing {self.func.__name__}...')
            setattr(obj, self.attr, self.func(obj))
        return getattr(obj, self.attr)

    def __set__(self, obj, value):
        setattr(obj, self.attr, value)

class DataAnalyzer:
    def __init__(self, data):
        self.data = data

    @cached_property
    def statistics(self):
        import statistics
        return {
            'mean': statistics.mean(self.data),
            'stdev': statistics.stdev(self.data)
        }

da = DataAnalyzer([1, 2, 3, 4, 5])
print(da.statistics)  # Computing... (first time)
print(da.statistics)  # Cached (no recompute)
```

cached_property computes on first access, then stores result in instance \__dict_\_. Python 3.8+ has functools.cached_property built-in.

## Key points

- functools.cached_property is the built-in cached property descriptor (Python 3.8+)
- Data descriptors (with \__set_\_) cannot be overridden by instance dict
- All Python classmethods, staticmethods, and properties are descriptors
- The lookup chain: data descriptors > instance dict > non-data descriptors

## Common follow-up questions

- What is the attribute lookup order in Python?
- How do functions become bound methods?
- What is the difference between data and non-data descriptors?

## Interview tips

- Explaining that functions are descriptors impresses senior interviewers
- The lookup priority (data > instance dict > non-data) is the core concept
- Show cached_property as a practical descriptor implementation

## Concepts covered

Descriptor, \__get_\_, \__set_\_, \__delete_\_, Data Descriptor, Non-data Descriptor

Previous: [namedtuple vs NamedTuple vs dataclass](https://www.internhack.in/learn/interview/python-interview/python-namedtuple) · Next: [Python Packaging and Project Setup](https://www.internhack.in/learn/interview/python-interview/python-packaging) · [All of Python Interview](https://www.internhack.in/learn/interview/python-interview)

## More Python Interview questions

- [Python Performance Profiling](https://www.internhack.in/learn/interview/python-interview/python-profiling)
- [TypeVar and Generic Programming](https://www.internhack.in/learn/interview/python-interview/python-typevar-generics)
- [String Formatting Methods](https://www.internhack.in/learn/interview/python-interview/python-string-formatting)
- [subprocess Module](https://www.internhack.in/learn/interview/python-interview/python-subprocess)
- [Python Data Model and \__dunder_\_ Methods](https://www.internhack.in/learn/interview/python-interview/python-datamodel)
- [Advanced Class Mechanics](https://www.internhack.in/learn/interview/python-interview/python-class-advanced)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [Python Interview](https://www.internhack.in/learn/interview/python-interview)
