Repaso
EspaƱol
PythonAdvancedPY-24

@property is built on which mechanism?

Options

  1. Metaclasses, which rewrite the class when it is created
  2. The descriptor protocol, using __get__ and __set__ (Correct option)
  3. The __getattr__ fallback, called for missing attributes
  4. 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.

Key terms

Practice this question in Repaso