29.6 Properties vs __getattr__: When Each Is Appropriate
In object-oriented Python, the mechanisms for controlling attribute access are powerful but distinct. Two primary tools for this are properties and the __getattr__ method. Understanding their fundamental differences—specifically, the distinction between data access via the descriptor protocol and via the lookup chain—is crucial for employing them correctly. Properties are a declarative tool built on Python’s descriptor protocol. They allow you to define getter, setter, and deleter methods for a specific, predefined attribute, transforming what looks like simple attribute access into method calls. This is ideal when you have a known attribute whose value you need to compute, validate, or otherwise control at the point of access.