@property is built on which mechanism?
Options
- Metaclasses, which rewrite the class when it is created
- The descriptor protocol, using
__get__and__set__(Correct option) - The
__getattr__fallback, called for missing attributes - Closures, which keep the getter's variables alive
Answer
The descriptor protocol (__get__ / __set__)
Why
A descriptor is an object that controls what happens when an attribute is read or written, by defining __get__, __set__ or __delete__.
property is a built-in descriptor: reading obj.x calls your getter function.
The same mechanism makes methods, classmethods and ORM columns (like SQLAlchemy fields) work.
Remember
@property is a descriptor.