The @property Decorator
Use property for getters, setters, and deleters.
Introduction
@property turns a method into a readable attribute. @setter and @deleter complete the controlled access pattern.
Why @property?
Instead of getX()/setX() Java-style, Python uses @property. Access looks like an attribute: obj.value, not obj.get_value().
class Circle:
def __init__(self, r): self._r = r
@property
def radius(self): return self._r
c = Circle(5)
print(c.radius) # no ()All lessons in this course
- Functions as First-Class Objects
- Writing Custom Decorators
- The @property Decorator
- @classmethod and @staticmethod