All courses › Intermediate Programming › @property

@property

@property turns a method into something that can be read like a plain attribute, without parentheses. It is useful when a value should be computed from other attributes, but used exactly as if it were stored directly.

@property@\text{property}makes obj.method readable without ()

Example

class Sirkel:

@property

def areal(self): return 3.14 * self.r**2

Sirkel(2).areal (no parentheses) gives the area.

Use @property for computed values — it keeps the interface simple even if the calculation changes later.
Practise object orientation for free →

← Encapsulation and polymorphism · Time complexity, O-notation →

Part of Intermediate Programming: Object orientation.