Menu
Coddy logo textTech

Property Decorator

Part of the Object Oriented Programming section of Coddy's Python journey — lesson 10 of 64.

The @property decorator allows you to access methods like attributes, providing a clean way to get and set values. This is useful for encapsulation, allowing you to add validation logic or computed values without changing the public interface of your class.

Here is an example of a class using @property:

class Circle:
    def __init__(self, radius):
        self._radius = radius
    
    @property
    def radius(self):
        return self._radius
    
    @property
    def area(self):
        return 3.14159 * self._radius ** 2

Note that we use self._radius to store the actual value. If we used self.radius inside the getter, it would call the getter method again, leading to infinite recursion.

Create a circle and access properties:

my_circle = Circle(5)

Access properties like regular attributes:

print(my_circle.radius)
print(my_circle.area)

Output:

5
78.53975

Notice you don't use parentheses () when accessing properties - they look like attributes but run method code.

Add a setter to allow changing values with validation:

class Circle:
    def __init__(self, radius):
        self._radius = radius
    
    @property
    def radius(self):
        return self._radius
    
    @radius.setter
    def radius(self, value):
        if value > 0:
            self._radius = value
        else:
            print("Radius must be positive!")

Now you can set the radius like an attribute:

my_circle = Circle(5)
my_circle.radius = 10  # Uses the setter
print(my_circle.radius)  # Uses the getter

Output:

10

Key Point: @property makes methods look like attributes, while @property_name.setter allows you to control how values are assigned, enabling data validation and encapsulation.

Try it yourself

This lesson doesn't include a code challenge.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming