The @property Decorator
Use property for getters, setters, and deleters.
The @property Decorator is a free Python Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Introduction
Why @property?
class Circle:
def __init__(self, r): self._r = r
@property
def radius(self): return self._r
c = Circle(5)
print(c.radius) # no ()Computed Properties
import math
class Circle:
def __init__(self, r): self._r = r
@property
def area(self): return math.pi * self._r**2
c = Circle(5)
print(round(c.area, 2))Property Setter
class Circle:
def __init__(self, r): self._r = r
@property
def radius(self): return self._r
@radius.setter
def radius(self, val):
if val <= 0: raise ValueError('radius must be positive')
self._r = val
c = Circle(5)
c.radius = 10
print(c.radius)Property Deleter
class User:
def __init__(self, name): self._name = name
@property
def name(self): return self._name
@name.deleter
def name(self): del self._name
u = User('Alice')
del u.name
print(hasattr(u, '_name'))Read-Only Property
class Const:
def __init__(self, v): self._v = v
@property
def value(self): return self._v
c = Const(42)
print(c.value)
try: c.value = 99
except AttributeError as e: print(e)Property with Validation
class BoundedInt:
def __init__(self, lo, hi): self.lo,self.hi=lo,hi; self._v=lo
@property
def value(self): return self._v
@value.setter
def value(self, v):
if not self.lo <= v <= self.hi:
raise ValueError(f'{v} out of [{self.lo},{self.hi}]')
self._v = v
b = BoundedInt(0, 10)
b.value = 5
print(b.value)Caching with property
class Expensive:
_cache = None
@property
def result(self):
if self._cache is None:
self._cache = sum(range(1000))
return self._cache
e = Expensive()
print(e.result)
print(e.result)cached_property (Python 3.8+)
from functools import cached_property
import math
class Circle:
def __init__(self, r): self.r = r
@cached_property
def area(self): return math.pi * self.r**2
c = Circle(5)
print(round(c.area, 2))Property vs __slots__
class Point:
__slots__ = ('_x', '_y')
def __init__(self, x, y): self._x,self._y=x,y
@property
def x(self): return self._x
p = Point(1, 2)
print(p.x)When to Use @property
# Before: class Person:
# def __init__(self, age): self.age = age
# After: add validation without changing caller code
class Person:
def __init__(self, age): self.age = age
@property
def age(self): return self._age
@age.setter
def age(self, v):
if v < 0: raise ValueError('negative age')
self._age = v
p = Person(30)
print(p.age)Quick Check
Recap
Keep Going
Frequently asked questions
Is the “The @property Decorator” lesson free?
Yes — the full text of “The @property Decorator” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.
What will I learn in “The @property Decorator”?
Use property for getters, setters, and deleters. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The @property Decorator” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Functions as First-Class Objects
- Writing Custom Decorators
- The @property Decorator
- @classmethod and @staticmethod