Fundamentals 11 min read

Understanding Python @property Decorator: Advantages, Syntax, and Usage

This tutorial explains the benefits of using Python properties, introduces decorator functions, and provides step‑by‑step examples of defining @property getter, setter, and deleter methods to create clean, readable, and maintainable class interfaces.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Understanding Python @property Decorator: Advantages, Syntax, and Usage

Welcome! In this article you will learn how to use the @property decorator in Python.

1️⃣ Advantages of Properties in Python

Properties offer a concise, readable syntax, allow attribute access like public fields while enabling validation through getter and setter middleware, and let you reuse the same name for getter, setter, and deleter, making code cleaner and more maintainable.

2️⃣ Introduction to Decorators

A decorator is a function that adds new functionality to another function without modifying its source. The following example shows a typical decorator:

<code>def decorator(f):
    def new_function():
        print("Extra Functionality")
        f()
    return new_function

@decorator
def initial_function():
    print("Initial Functionality")

initial_function()</code>

Output:

<code>Extra Functionality
Initial Functionality</code>

The decorator wraps initial_function , executing extra code before the original function runs.

3️⃣ Using @property in Real Scenarios

Consider a House class that initially stores a public price attribute:

<code>class House:
    def __init__(self, price):
        self.price = price</code>

To protect the attribute and validate new values, you can replace direct access with a property.

4️⃣ Property Syntax and Logic

Defining a property involves three optional methods:

Getter – returns the attribute value.

Setter – validates and sets a new value.

Deleter – removes the attribute.

Example implementation:

<code>class House:
    def __init__(self, price):
        self._price = price

    @property
    def price(self):
        return self._price

    @price.setter
    def price(self, new_price):
        if new_price > 0 and isinstance(new_price, float):
            self._price = new_price
        else:
            print("Please enter a valid price")

    @price.deleter
    def price(self):
        del self._price</code>

Getter

The getter simply returns self._price :

<code>@property
def price(self):
    return self._price</code>

Usage:

<code>house = House(50000.0)
print(house.price)  # 50000.0</code>

Setter

The setter validates that the new price is a positive float before assigning it:

<code>@price.setter
def price(self, new_price):
    if new_price > 0 and isinstance(new_price, float):
        self._price = new_price
    else:
        print("Please enter a valid price")</code>

Usage:

<code>house.price = 45000.0
print(house.price)  # 45000.0</code>

Invalid values trigger the error message and leave the original price unchanged.

Deleter

The deleter removes the protected attribute:

<code>@price.deleter
def price(self):
    del self._price</code>

Attempting to access house.price after deletion raises an AttributeError .

Final Tips

You can define only a getter for read‑only properties.

Define getter and setter without a deleter if deletion isn’t needed.

Choose the combination of methods that fits your class design.

Summary

Using @property makes property definitions concise and Pythonic.

It provides a clean way to implement getters, setters, and deleters.

Properties let you modify internal implementation without changing external code, improving maintainability.

pythonoopdecoratorpropertygettersetter
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.