The property() constructs in Python returns the property attribute.
Python property() is a built-in function that returns a property attribute. The property() method delivers the property attribute from a given getter, setter, and deleter.
If no arguments are given, the property() method returns the base property attribute that doesn’t include any getter, setter, or deleter. If the doc isn’t provided, the property() method takes the docstring of the getter function.
The syntax of the Python property() method is the following.
property(fget=None, fset=None, fdel=None, doc=None)
The property() method takes four optional parameters:
- fget (Optional) – This function for getting the attribute value.
- fset (Optional) – This function for setting the attribute value.
- fdel (Optional) – This function for deleting the attribute value.
- doc (Optional) – The attribute’s string contains the documentation (docstring).
See the following example.
# app.py class Student: def __init__(self, fname): self._fname = fname def getfname(self): print('Getting fname') return self._fname def setfname(self, value): print('Setting fname to ' + value) self._fname = value def delfname(self): print('Deleting fname') del self._fname fname = property(getfname, setfname, delfname, 'fname property') s = Student('Jane') print(s.fname) s.fname = 'Eleven' del s.fname
See the output.
➜ pyt python3 app.py Getting fname Jane Setting fname to Eleven Deleting fname ➜ pyt
Here, _fname is used as the private variable for storing the fname of a Student.
We also set:
- a getter method
getfname()to get the name of the Student,
- a setter method
setfname()to set the name of the Student,
- a deleter method
delname()to delete the name of the Student.
Now, we set the new property attribute name by calling the property() method.
As shown in the program, referencing
s.fname internally calls
getfname() as getter,
setfname() as setter and
delfname() as deleter through the printed output present inside the methods.
The @property decorator allows us to define properties easily without manually calling a property() function. Before studying the @property decorator, let’s understand what a decorator is.
#What is a decorator
In Python, a function is a first-order object. It means that it can be passed as an argument to another function.
It is also possible to define the function inside another function. Such a function is called the nested function. Moreover, the function can return another function.
A decorator is a function that receives another function as an argument. A decorator extends the behavior of an argument function without actually modifying it. So, that is it for a decorator.
Instead of using a property() method, you can use the Python decorator @property to assign a getter, setter, and deleter. See the following code.
# app.py class Student: def __init__(self, fname): self._fname = fname @property def fname(self): print('Getting fname') return self._fname @fname.setter def fname(self, value): print('Setting fname to ' + value) self._fname = value @fname.deleter def fname(self): print('Deleting fname') del self._fname s = Student('Enola') print('The fname is:', s.fname) s.fname = 'Millie' del s.fname
See the output.
➜ pyt python3 app.py Getting fname The fname is: Enola Setting fname to Millie Deleting fname ➜ pyt
Here, instead of using a property() method, we’ve used the
First, we specify that
fname() method is also an attribute of a Student. It is done using the @property before the getter method, as shown in the program.
Next, we use an attribute name to specify the setter and the deleter.
This is done by using @<name-of-attribute>.setter
(@fname.setter) for setter method and @<name-of-attribute>.deleter
(@fname.deleter) for deleter method.
Notice that we’ve used the same method
fname() with different definitions for defining the getter, setter, and deleter.
Now, whenever we use
s.fname, it internally calls the appropriate getter, setter, and delete, as shown by the printed output inside the method.
That’s it for this tutorial.