Property
In one line
@property lets you access a method like an attribute, enabling validation, computation, or encapsulation.
In simple words
The @property decorator turns a method into something that's accessed like an attribute (no parentheses). This lets you:
- Compute values on access —
circle.areainstead ofcircle.area() - Add validation — check values when setting
- Encapsulate — change internal storage without changing the public API
- Make read-only attributes — define getter without setter
Use @property for the getter, @x.setter for the setter, @x.deleter for deletion. This is Python's way of doing getter/setter without Java-style verbosity.
Example
class Temperature:
def __init__(self, celsius=0):
self.celsius = celsius # Goes through setter
@property
def fahrenheit(self):
return self._celsius * 9/5 + 32
@fahrenheit.setter
def fahrenheit(self, value):
self._celsius = (value - 32) * 5/9
@property
def celsius(self):
return self._celsius
@celsius.setter
def celsius(self, value):
if value < -273.15:
raise ValueError("Below absolute zero!")
self._celsius = value
t = Temperature(100)
print(t.fahrenheit) # 212.0 — looks like attribute access
t.fahrenheit = 32
print(t.celsius) # 0.0@property makes method access look like attribute access. The setter validates input — trying to set below -273.15 raises ValueError.
Common confusions
Confused with: attribute
The difference: A plain attribute is simple storage (obj.x = 5). A property is a method disguised as an attribute — it can compute, validate, or transform on every access/set. Properties let you add logic later without changing the public API.
Related terms
Related Resources
Related Lessons
- Python OOP
Properties.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.