Meaning
The @property decorator transforms a method into a getter for a read-only attribute, or with setter and deleter methods, into a managed attribute. It addresses the need for controlled attribute access (e.g., validation, lazy loading) without sacrificing the simplicity of attribute syntax. Developers reach for it when they want to encapsulate attribute access logic while providing a clean public interface.
Primary Function
Attribute management
Communicative Purpose
Enables managed attribute access (e.g., validation, lazy computation) while preserving the simplicity of attribute dot notation.
Pattern
@property
Core Structure
@property
Função primária
Attribute management
Propósito comunicativo
Enables managed attribute access (e.g., validation, lazy computation) while preserving the simplicity of attribute dot notation.
Situações de gatilho
Python: validating attribute values on assignment (e.g., ensuring age is non-negative) Python: computing expensive values only when accessed (lazy evaluation) Python: maintaining consistency between related attributes (e.g., updating area when width changes)
Contextos
Python class design, data models, GUI frameworks, libraries requiring encapsulation.
Padrão
@property
Estrutura central
@property
Colocados típicos
- @setter
- @deleter
- private attributes (e.g.
- _value)
Substituições comuns
- Using getter/setter methods (e.g.
- get_value()
- set_value()) — more verbose and less Pythonic
- using __getattr__/__setattr__ — can be overkill and less explicit.
Erros comuns
Forgot to decorate the setter/deleter with @property.setter/@property.deleter — causes AttributeError when trying to set/delete Using @property on a method that returns None or has side effects — breaks the expectation of a property as a value Making the property computationally expensive without caching — leads to performance issues on repeated access Confusing the property name with the underlying private attribute name (e.g., self.value instead of self._value) — causes recursion Using properties for complex logic that should be a method — violates the principle that properties should feel like attributes
Similar / contraste
@setter/@deleter: used alongside @property to define setter and deleter for the same attribute; __slots__: restricts attributes to a fixed set for memory efficiency, not for access control
Interferências
Coming from Java: may use explicit getter/setter methods (getX/setX) — Python's @property provides a more seamless attribute access syntax; Coming from C#: may expect properties to be fields with backing store — Python properties are methods that look like attributes
Família do chunk
- @setter
- @deleter
- @classmethod
- @staticmethod
Nuance
Do not use for simple public attributes that need no logic — adds unnecessary complexity; Properties can impact performance if accessed frequently in tight loops due to function call overhead; Properties are not data descriptors unless they also define __set__ or __delete__, affecting attribute lookup precedence.
Efeito pragmático
Encapsulates attribute access logic, allowing changes to implementation without breaking external code that uses the attribute syntax.
Dica de memória
Think of @property as a smart gatekeeper: it lets you interact with an attribute as if it were a simple variable, but secretly runs validation or computation behind the scenes.
Upgrade path
Learn @setter and @deleter to create fully managed attributes with validation and lazy computation.
Log in to save chunks.