Updated April 11, 2023
Introduction to Python Property()
In Python, there is an in-built function for defining the properties of the program class and this function is known as property() function. The property() function in Python returns property attributes from getter, setter, and deleter. The property object provides these methods such as getter(), setter() and delete(). In Python, there are other ways of assigning these getter, setter, and delete using Python decorator symbolized as @property instead of using property() function. The @property decorator is a built-in decorator in Python to define the name property in the given class. In this article, we will see syntax and examples in the below section.
Working of Property() Function with Syntax
In this section, Python provides a built-in function for defining or creating the properties of a class that can return a property attribute from the given getter, setter, and deleter. Now let us see below the syntax for property() function.
Syntax:
property (fget, fset, fdel, doc)
Parameters:
- fget(): as the name, it is used for fetching the attribute value.
- fset(): this is used for setting the attribute value.
- fdel(): this is used for deleting the attribute value.
- doc: this parameter is used for specifying the documentation for the attribute.
Examples of Python property()
Below are the examples mentioned:
Example #1
Code:
print("Program for class segment of employee and his details:")
print("\n")
class emp:
def __init__(self, name, salary):
self.name = name
self.salary = salary
self.obtainsalary = self.name + ' obtained ' + self.salary + ' salary'
e = emp("Allen", "25000")
print('Emplyoee name is ' + e.name)
print('Salary is ' + e.salary)
print(e.obtainsalary)
Output:
We can see in the above output we have only details of ‘Allen’ now we want it to update for another employee ‘Daniel’ so to do this we need to add 3 more lines to the above program
print("Program for class segment of employee and his details:")
print("\n")
class emp:
def __init__(self, name, salary):
self.name = name
self.salary = salary
self.obtainsalary = self.name + ' obtained ' + self.salary + ' salary'
e = emp("Allen", "25000")
print('Emplyoee name is ' + e.name)
print('Salary is ' + e.salary)
print(e.obtainsalary)
e.name = "Daniel"
print('Employee name is ' + e.name)
print(e.obtainsalary)
Output:
So we can see that the above output updates employee name only but the sentence ‘Allen obtained 25000 salary remains the same. So to avoid such problems we can use python property() function. So we can define the ontainsalary() function and it can be shown in the below screenshot and the updated output is as follows and the code is as follows.
print("Program for using python property() function:")
print("\n")
class Emp:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def obtainsalary(self):
return self.name + ' obtained ' + self.salary + ' salary'
e = Emp("Allen", "25000")
print('Employee name is ' + e.name)
print('Employee salary is ' + e.salary)
print(e.obtainsalary())
e.name = "Daniel"
print('Employee name is ' + e.name)
print(e.obtainsalary())
Output:
In Python, property() function it provides three different functions such as getter(), setter() and deleter(). We should note that if arguments are not passed to the property() function which returns base property attribute which does not contain any getter, setter, and deleter methods, and if the doc is not provided property() function takes the docstring of the getter function. Let us consider the below example for demonstrating these three methods of property() function.
Example #2
Code:
print("Program to demonstrate property() function:")
print("\n")
class Animals:
def __init__(self, name):
self._name = name
def getAn(self):
print('Fetching the given name')
return self._name
def setAn(self, value):
print('Setting the name obtained to ' + value)
self._name = value
def delAn(self):
print('Deleting the set name')
del self._name
name = property(getAn, setAn, delAn, 'Animal Name property')
a = Animals('Tiger')
print(a.name)
a.name = 'Lion'
del a.name
Output:
In the above program, we can see we have defined a class ‘Animal’ where we are defining the name of the animal which is a property of class Animal. Then we get the name, we are setting the name and deleting the name to do this we are defining getAn(), setAn() and delAn() functions. In the above program, we use _name as a private variable of class Animal to store the name of the animals.
In Python, we can also use property decorators such as @property instead of using property() function which works similarly as property() function. The property decorator is also a built-in which provides the easiest way of using getter and setter methods and its main work is to add functionalities to the existing program which is also known as metaprogramming, in general, this means a part of the program allows to change another part of the program at compile time is known as metaprogramming. Now let us demonstrate below with an example for a property decorator.
Example #3
Code:
print("Program to demonstrate the @property decorator is as follows:")
print("\n")
class Animals:
def __init__(self, name):
self._name = name
@property
def name(self):
print('Fetching the name')
return self._name
@name.setter
def name(self, name):
print('Setting the name to ' + name)
self._name = name
@name.deleter
def name(self):
print('Deleting the set name')
del self._name
a = Animals('Tiger')
print(a.name)
a.name = 'Lion'
del a.name
Output:
In the above program, we can see first we have defined a class Animals same as above example for property() function. This example also works same as property() function in the above program the name method is also an attribute to the defined class and we can notice that this same method name() is used for defining the setter, getter and delete methods in the above program using the @property decorator as @name therefore whenever we call a.name it internally calls the respective getter, setter and deleter methods. Therefore @property decorator provides the easiest way than property() function to declare a property instead of calling the property() function.
Conclusion
In this article, we conclude that in python there is built-in function property() that is used for defining the property of the declared class. This function in Python has a decorator that works similarly to property() function is a property decorator known as @property. In this article, we also saw when to use property() function for example. Then we saw proper syntax and examples of property() function. In this we also saw using property decorator is the easiest way than using property() function for defining getter, setter, and deleter methods of property function.
Recommended Articles
This is a guide to Python Property(). Here we discuss the working of Python Property() function with appropriate syntax and respective examples. You may also have a look at the following articles to learn more –