Python property() function is used to create and manage properties of a class.
Syntax
property(fget=None, fset=None, fdel=None, doc=None)
Parameters
- fget (Optional) – This function is for getting the attribute value. Default is None.
- fset (Optional) – This function is for setting the attribute value. Default is None.
- fdel (Optional) – This function is for deleting the attribute value. Default is None.
- doc (Optional) – The attribute’s string contains the documentation (docstring). Default is None.
Return value
It returns a property object.
Example 1: Using property() directly
class NetflixSeries:
def __init__(self, name):
self._name = name
def get_name(self):
print("Get method is called")
return self._name
def set_name(self, name):
print("Set method is called")
self._name = name
def del_name(self):
print("Del method is called")
del self._name
# Creating a property object 'name'
# It defines getter, setter, and deleter for the '_name' attribute
name = property(get_name, set_name, del_name)
# Creating an instance of NetflixSeries
series = NetflixSeries('Breaking Bad')
# Accessing the name property (triggers the get_name method)
print(series.name)
# Modifying the name property (triggers the set_name method)
series.name = 'Suits'
# Accessing the updated name property (triggers the get_name method)
print(series.name)
# Deleting the name property (triggers the del_name method)
del series.name
Output
Get method is called
Breaking Bad
Set method is called
Get method is called
Suits
Del method is called
Example 2: Using @property decorator
The @property decorator allows to define properties without manually calling a property() function and it also allows for the assignment of getter, setter, and deleter functions.
class NetflixSeries:
def __init__(self, name):
self._name = name
@property
def name(self):
print("Get method is called")
return self._name
@name.setter
def name(self, name):
print("Set method is called")
self._name = name
@name.deleter
def name(self):
print("Del method is called")
del self._name
# Creating an instance of NetflixSeries
series = NetflixSeries('Breaking Bad')
# Accessing the name property (triggers the getter)
print(series.name)
# Modifying the name property (triggers the setter)
series.name = 'Suits'
# Accessing the updated name property (triggers the getter)
print(series.name)
# Deleting the name property (triggers the deleter)
del series.name
Output
Get method is called
Breaking Bad
Set method is called
Get method is called
Suits
Del method is called
The @property decorator is used to define the getter method for name.
The @name.setter decorator is used to define the setter method for name.
The @name.deleter decorator is used to define the deleter method for name.
This approach is more concise and is the recommended way to define properties in modern code.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.