---
title: "How the @property decorator work in Python?"  
description: "How the @property decorator work in Python?"  
author: "Utpal Vishwas"  
published: 2023-04-11  
updated: 2023-04-19  
canonical: https://www.mindstick.com/forum/157754/how-the-property-decorator-work-in-python  
category: "python"  
tags: ["python", "python 2"]  
reading_time: 2 minutes  

---

# How the @property decorator work in Python?

How the @[property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) decorator work in [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python)?

## Replies

### Reply by Aryan Kumar

In Python, the **@property** decorator is used to define a method as a getter method for a class attribute. It allows you to access the attribute using the dot notation as if it were a regular property, while also allowing you to add custom logic to the attribute's getter method.

Here's an example of how the **@property** decorator works:

```python
class Rectangle:
   def __init__(self, width, height):
       self._width = width
       self._height = height
   @property
   def width(self):
       return self._width
   @width.setter
   def width(self, value):
       if value <= 0:
           raise ValueError("Width must be positive")
       self._width = value
   @property
   def height(self):
       return self._height
   @height.setter
   def height(self, value):
       if value <= 0:
           raise ValueError("Height must be positive")
       self._height = value
   @property
   def area(self):
       return self._width * self._height
```

### Reply by Krishnapriya Rajeev

In Python, a decorator encloses a function, adds a number of features, and then returns the result.

The @property decorator in Python is used to define a method as a *read-only attribute* of a class. It allows us to access a method like an attribute, without the need to invoke it like a function.

Here's an example to illustrate the usage of @property decorator:

```plaintext
class Person:
   def __init__(self, name, age):
       self._name = name
       self._age = age

   @property
   def age(self):
       return self._age

   @property
   def name(self):
       return self._name

   @name.setter
   def name(self, name):
        self._name = name
```

Here's how the @property decorator works:

1. First, a method is defined inside a class, and the @property decorator is added before the method definition.
2. The @property decorator tells Python that the method is a getter method, which means that it will be used to retrieve the value of a private class variable.
3. The method is given a name that matches the name of the private variable it will be used to retrieve.
4. The private variable must be defined outside the method, usually at the beginning of the class definition, and must be prefixed with an underscore to indicate that it is a private variable.


---

Original Source: https://www.mindstick.com/forum/157754/how-the-property-decorator-work-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
