What is a Python property() Function?
There are plenty of built-in functions in python programing language, and the property() function is one of those. The python property() function is used to create a property of a specified class. It is used to create the managed attributes of a class commonly known as properties. The python property() function is the quickest way to create the managed attributes of a class. It takes 4 arguments, getter, setter, deleter, and doctype, which are explicitly used to create a property of a class. If no argument out of these four is passed to the property() function, the function will return a base property attribute without any setter, getter, or deleter. If the doc argument is not passed to the function, then the property() function will take the getter function’s doc string.
Syntax of the Python property() Function
When you get to learn a new function, you should get familiar with the syntax of that function first. The syntax of the python property() function is straightforward, and you can quickly understand and will remember it. The syntax of the python property() function is as follows:
The python property() function takes 4 input parameters, get, set, del, and doc. The get argument is used to get the attribute’s value, the set parameter is used to set the attribute’s values, the del parameter is used to delete the value of the attribute, and the doc is used to represent the documentation string for the attribute. The python property() function will return a property attribute created by the setter, getter, and deleter arguments. Now let us explore some examples to see the working of the property() function.
Example 1
In this example, we are going to set the name of a person with the getter and setter functions. The property() function will utilize the getter and setter attributes to create a property of the specified class. The sample code is given below to help you understand how you can use the getter and setter arguments in the property() function to create a property of the specified class. Check the sample code given below:
def __init__(self):
self.__name=''
def setter(self, name):
print('The name is set with setter function')
self.__name=name
def getter(self):
print('The name is gotten with getter function')
return self.__name
name=property(getter, setter)
p1=person()
p1.name="Kalsoom"
p1.name
The program starts with a class “person”, and under that class, we have a reserve method in the python class “__init__”, which is the constructor of the objects. The setter function takes two arguments, self and name. When the “name” parameter is passed to the setter function, the name will be assigned to the variable “name”. The getter function takes only one parameter, and that is “self”. This is used to get the value for the “name” parameter. And finally, we have the property function, which uses the getter and setter functions as parameters.
Once all these functions are placed in the class “person”, we need to explicitly call the person class to execute getter and setter functions. The “person()” calling is used to call the class with the variable “p1”. The name “Kalsoom” is passed to the class to set it as a “name”. To see the output of this program, refer to the screenshot given below:
Example 2
In the previous example, we learned how to get or set a value for the variable in the main class and create a property object of the class with the property() function. In this example, we will learn how to delete a variable and its value by using the “deleter” function. Refer to the sample code given in the screenshot below to learn how to define the “deleter” function in your program.
def __init__(self):
self.__name=''
def setter(self, name):
print('The name is set with setter function')
self.__name=name
def getter(self):
print('The name is gotten with getter function')
return self.__name
def deleter(self):
print('The name is Deleted with deleter function')
return self.__name
name=property(getter, setter, deleter)
p1=person()
p1.name="Kalsoom"
p1.name
del p1.name
The getter and setter functions have been explained in the previous example, so they are the same here as well. The newly added function is “deleter”, which is used to delete the attribute’s value. Now the “deleter” function is also passed to the property() function as a parameter so that we can delete an attribute value using the del command. To get the output of the above code, refer to the screenshot of the output given below:
Here, you can see that all three functions are called. The name is set with the setter function. To get the name, we used the getter function, and finally, by using the deleter function, we deleted the name.
Example 3
The python property() function takes four parameters, setter, getter, deleter, and the doc name. In the first example, we used the getter and setter functions as parameters. In the second example, we used the deleter function as well as a parameter. The fourth parameter, “doc name,” is left, so let us pass that as well to the property() function in this example. The doc file name has a “None” default value. Here we will specifically provide the doc name parameter to the property() function. Refer to the program given below to learn how to pass the fourth parameter to the function.
def __init__(self, name):
self.__name=name
def getter(self):
print('Getting the person\'s name')
return self.__name
def setter(self, v):
print('Setting the name to: '+ v)
self.__name = v
def deleter(self):
print('Name deleted')
return self.__name
name=property(getter, setter, deleter, 'Property name')
p1=person('Kalsoom')
print('The name is = ',p1.name)
p1.name= 'Ceily'
del p1.name
As you can observe, the code is the same as written in the previous examples; only the fourth parameter is provided to the python property() function. To view the output, refer to the screenshot given below:
Conclusion
This post was about the python property() function. The python property() function is used to construct the attributes of a property. It is a very helpful function as it allows you to create a property of a specified class just by using the property() function. It takes four arguments, getter, setter, deleter, and “doc name”. The getter function is used to obtain the value of an attribute. The setter function is used to set a value of an attribute. The deleter function is used to delete a value of an attribute. And the last parameter, the “doc name”, is used to provide the name of the document, which has a default “Null” value. By following the examples, you can easily learn how to define the property() function in python programs.