0Pricing
Python Academy · Lesson

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

@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 ()

Computed Properties

@property methods compute on-the-fly. obj.area calculates every time it is accessed — no stored value needed.
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

@property_name.setter adds a setter. Now c.radius = 10 calls the setter method.
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

@property_name.deleter handles del obj.attr. Use for cleanup logic.
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

Define only the getter (no setter). Attempting to assign raises AttributeError.
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

Setters are the perfect place to validate data — check type, range, or format before storing.
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

Combine @property with a cached value to compute once and memoize.
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+)

@functools.cached_property computes the property once and stores it as an instance attribute — no repeated computation.
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__

__slots__ prevents arbitrary attribute creation and saves memory. Properties add logic. They complement each other.
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

Use @property when: you need validation, computed values, or to maintain backward compatibility while changing internals.
# 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

What happens when you try to assign to a read-only @property (no setter defined)?

Recap

@property: getter becomes attribute access. @name.setter adds write support. @name.deleter adds del support. @cached_property computes once. Setters are for validation.

Keep Going

Excellent progress! Keep going to master the next concept.

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

  1. Functions as First-Class Objects
  2. Writing Custom Decorators
  3. The @property Decorator
  4. @classmethod and @staticmethod
← Back to Python Academy