Descriptor Protocol for Type Safety
Descriptors are classes that define any of the methods __get__, __set__, or __delete__. They are used to implement custom logic for attribute access. In this snippet, __set_name__ automatically captures the attribute name, and __set__ enforces that the value assigned to 'age' is always an integer.
class IntegerField:def __set_name__(self, owner, name):self.name = namedef __set__(self, instance, value):if not isinstance(value, int):raise ValueError(f'{self.name} must be an integer')instance.__dict__[self.name] = valueclass User:age = IntegerField()