← All Exercises

Python Descriptor Protocol

advancedpython~35min

Background

Descriptors power Python property, classmethod, and ORM field implementations through the attribute access protocol.

Problem

Create a TypedField descriptor that validates the type of assigned values and raises TypeError on mismatch.

Input

A class using the TypedField descriptor with a specified expected type.

Output

A class attribute that validates type on assignment.

Sample tests

Sample 1

Input: obj.name = "Alice" (str field)
Expected: Assignment succeeds

Your solution

Learning objective

Understand the descriptor protocol (__get__, __set__, __set_name__) and attribute access interception.