
This Blog deals with A decorator feature in Python wraps in a function, appends several functionalities to existing code and then returns…
- A pythonic way to use getters and setters in object-oriented programming.
- It is a built-in property provided by python.
It creates and returns a property object. The syntax of this function is:

- where,
- fget is function to get value of the attribute
- fset is function to set value of the attribute
- fdel is function to delete the attribute
- doc is a string (like a comment)
You are modeling a employee salary with a Employee class (at the moment, the class has a fname, lastname and emp_salary instance attribute defined):

This instance attribute is public because its name doesn’t have a leading underscore. Since the attribute is currently public, it is very likely that it is accessed and modified the attribute directly in other parts of the program using dot notation.
The property Class
- A pythonic way to deal with the above problem is to use the property class. Here is how we can update our code.
- we reuse the new_salary name while defining our getter and setter functions.

- The above implementation is simple and efficient. It is the recommended way to use property.
Advantages
- Syntax used is very concise and readable.
- Access instance attributes exactly as if they were public attributes while using the “magic” of intermediaries (getters and setters) to validate new values and to avoid accessing or modifying the data directly.
- Using @property, you can “reuse” the name of a property to avoid creating new names for the getters, setters, and deleters.