📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Python from Zero Property and Descriptors

Property and Descriptors

6 min read
@property turns a method into an attribute. Add @name.setter to allow assignment with validation. Computed properties like area = width * height hide implementation details behind a clean interface.

@property Decorator

class Temperature:
    def __init__(self, celsius):
        self._c = celsius

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

    @celsius.setter
    def celsius(self, value):
        if value < -273.15:
            raise ValueError("Below absolute zero!")
        self._c = value

    @property
    def fahrenheit(self):
        return self._c * 9/5 + 32

t = Temperature(100)
print(t.fahrenheit)  # 212.0