@property def getter_name(self): return self.attribute
Decorators & Metaclasses

Meaning

The @property decorator converts a zero-argument method into a computed attribute, accessible without parentheses like a plain attribute. It solves the problem of breaking the public API when evolving a simple attribute into a computed or validated one. You reach for it when callers must access a derived or validated value using attribute syntax rather than an explicit method call.

Primary Function

Encapsulation

Communicative Purpose

Enables computed or validated attribute access with plain attribute syntax, preserving API stability across implementation changes

Pattern

@property def getter_name(self): return self.attribute

Core Structure

@property def ...(self): return ...

Função primária

Encapsulation

Propósito comunicativo

Enables computed or validated attribute access with plain attribute syntax, preserving API stability across implementation changes

Situações de gatilho

OOP design: exposing a derived value such as a full name from first and last name components. Data validation: intercepting attribute reads to compute or enforce constraints before returning. API stability: migrating a stored attribute to a computed one without breaking existing caller code.

Contextos

Python class definitions, Django model properties, dataclass computed fields, Pydantic model properties

Padrão

@property def getter_name(self): return self.attribute

Estrutura central

@property def ...(self): return ...

Slots de substituição

getter_name: valid Python identifier used as the attribute name, attribute: any expression or class attribute to return

Colocados típicos

  • @getter_name.setter
  • @getter_name.deleter
  • property()
  • functools.cached_property
  • @property\ndef full_name(self):\n return f'{self.first} {self.last}'getattribute@property\ndef full_name(self):\n return f'{self.first} {self.last}'

Substituições comuns

  • property(getter
  • setter): older explicit form with same behavior but less readable. functools.cached_property: caches result after first call
  • avoids recomputation on repeated access. Regular method call obj.get_name(): explicit but forces parentheses at every call site. @property\ndef full_name(self):\n return f'{self.first} {self.last}'getattr@property\ndef full_name(self):\n return f'{self.first} {self.last}': intercepts all missing attributes
  • broader but less targeted than a named property.

Erros comuns

Forgetting @property and calling the method with parentheses, causing TypeError when treating the return value as an attribute. Defining a setter with a mismatched name so @full_name.setter is silently ignored and assignment raises AttributeError. Omitting the return statement, making the property silently evaluate to None. Performing expensive I/O or network calls inside @property, hiding latency behind what callers assume is a cheap attribute read.

Similar / contraste

functools.cached_property: caches result after first access, @property recomputes every time. @property\ndef full_name(self):\n return f'{self.first} {self.last}'getattr@property\ndef full_name(self):\n return f'{self.first} {self.last}': catches all missing attributes broadly, @property targets one specific name. Regular method: requires parentheses, @property does not. Descriptor protocol: lower-level mechanism, @property is a built-in descriptor shortcut.

Interferências

Coming from Java: may write explicit getFullName() methods instead of @property, missing Python's idiomatic attribute-style access. Coming from C#: may expect auto-backed properties with implicit storage, but Python @property requires explicit return logic. Coming from JavaScript: may confuse @property with getter syntax inside object literals, which use the get keyword not a decorator.

Família do chunk

  • @property getter
  • @property setter
  • @property deleter
  • functools.cached_property
  • property() builtin
  • descriptor protocol

Nuance

Do NOT use @property for expensive operations such as network calls or heavy computation because callers assume attribute access is cheap and fast. Every access re-executes the method body with no implicit caching. Properties are data descriptors and their @property\ndef full_name(self):\n return f'{self.first} {self.last}'set@property\ndef full_name(self):\n return f'{self.first} {self.last}' takes precedence over instance @property\ndef full_name(self):\n return f'{self.first} {self.last}'dict@property\ndef full_name(self):\n return f'{self.first} {self.last}', which can cause subtle surprises if you try to set an instance attribute with the same name.

Efeito pragmático

Allows transparent migration from stored attributes to computed or validated ones without breaking any caller code, preserving backward compatibility in public APIs.

Dica de memória

Like a vending machine button that looks like a simple switch but runs a hidden computation each time you press it — same push, fresh result every time.

Nota

A property object is a data descriptor, meaning it overrides instance dictionary entries for the same key, unlike non-data descriptors such as plain methods.

Upgrade path

@getter_name.setter for read-write properties; functools.cached_property for memoized computed attributes; custom descriptor classes for reusable property logic

Frequência: HighFormulaicidade: Semi-fixedTipo de construção: decoratorPrioridade de aquisição: Active recallPrioridade de output: OutputTag de espaçamento: Short-term

Log in to save chunks.