The property() is a built-in function in Python that returns the object of the property class. In other words, it is used to create the property of a class. However, we can optionally provide functions to get, set, or delete the attribute value.
Python property function:
Signature
The signature for the property()
function is as shown below.
property(fget=None, fset=None, fdel=None, doc=None)
Parameters and return type
- This method takes four optional parameters. Here, the
fget
is a function that gets the attribute value whereas thefset
is a function that sets the attribute value. Thereafter, thefdel
is a function that deletes the attribute value and thedoc
is a string containing documentation. The default value is none for all these parameters. Moreover, If arguments are not given,property()
method returns a base property attribute that doesn’t contain any get, set or delete methods. - However, it returns the property attributes formed by given get, set, and delete functions.
Python Built-in Function property Examples:
Example 1: For example, let us take a class student with an attribute department. Thereafter, we will write three methods to get, set and delete the value of the attribute department. Here, we can see when we get or set a value, it automatically calls the methods of the student class.
# student class
class student:
def __init__(self, value):
self._dept = value
# getting the values
def getdepartment(self):
print('Getting value')
return self._dept
# setting the values
def setdepartment(self, d):
print('Setting value to ' + d)
self._dept = d
# deleting the values
def deldepartment(self):
print('Deleting value')
del self._dept
dept = property(getdepartment, setdepartment, deldepartment )
# creating an instance
x = student('computer')
print(x.dept)
#setting the value
x.dept = 'IT'
#Deleting
del x.dept
Output
Getting value
computer
Setting value to IT
Deleting value
Example 2: For example, let us take class marks with attribute sports. Thereafter, we will write three methods to get, set and delete the value of the attribute department. Here, we can see when we get or set a value, and it automatically calls the methods of the marks class.
# marks class
class marks:
def __init__(self, value):
self._sports = value
# getting the values
def getmarks(self):
print('Getting value')
return self._sports
# setting the values
def setmarks(self, d):
print('Setting value to ',d)
self._sports = d
# deleting the values
def delmarks(self):
print('Deleting value')
del self._sports
sports = property(getmarks, setmarks, delmarks)
# creating an instance
x = marks(80)
print(x.sports)
#setting the value
x.sports = 85
#Deleting
del x.sports
Output
Getting value
80
Setting value to 85
Deleting value
Example 3: For example, let us take a class area with an attribute radius. Thereafter, we will write three methods to get, set and delete the value of the attribute department. Here, we can see when we get or set a value, it automatically calls the methods of the area class. Here, we are computing area of a circle from the radius.
# area class
class area:
def __init__(self, value):
self._radius= value
# getting the values
def getradius(self):
return self._radius
# setting the values
def setradius(self, d):
print('Setting value to ', d)
self._radius = d
# deleting the values
def delradius(self):
print('Deleting value')
del self._radius
radius = property(getradius, setradius, delradius )
# creating an instance
x = area(12.5)
print("Area of circle for radius ",x.radius," is ",(3.14*x.radius*x.radius))
#Changing the radius to 13 and computing area once again
x.radius= 13
print("Area of circle for radius ",x.radius," is ",(3.14*x.radius*x.radius))
#Deleting
del x.radius
Output
Area of circle for radius 12.5 is 490.625
Setting value to 13
Area of circle for radius 13 is 530.66
Deleting value
Conclusion
The property() function returns a property class object. However, in the presence of parameters, it overwrites the property class method.
References
Happy Learning 🙂