Python property() function - GeeksforGeeks

23 Sep, 2021
Python property() function returns the object of the property class and it is used to create property of a class. 
Syntax: property(fget, fset, fdel, doc)
Parameters: 
fget() – used to get the value of attribute
fset() – used to set the value of attribute
fdel() – used to delete the attribute value
doc() – string that contains the documentation (docstring) for the attribute
Return: Returns a property attribute from the given getter, setter and deleter.
Note: 
If no arguments are given, property() method returns a base property attribute that doesn’t contain any getter, setter or deleter.
If doc isn’t provided, property() method takes the docstring of the getter function.
Python3
# Python program to explain property() function# Alphabet class class Alphabet:    def __init__(self, value):        self._value = value     # getting the values    def getValue(self):        print('Getting value')        return self._value     # setting the values    def setValue(self, value):        print('Setting value to ' + value)        self._value = value     # deleting the values    def delValue(self):        print('Deleting value')        del self._value     value = property(getValue, setValue,                     delValue, )  # passing the valuex = Alphabet('GeeksforGeeks')print(x.value) x.value = 'GfG' del x.value
Output: 
Getting value
GeeksforGeeks
Setting value to GfG
Deleting value
The main work of decorators is they are used to add functionality to the existing code. Also called metaprogramming, as a part of the program tries to modify another part of the program at compile time.
Python3
# Python program to explain property()# function using decorator class Alphabet:    def __init__(self, value):        self._value = value     # getting the values    @property    def value(self):        print('Getting value')        return self._value     # setting the values    @value.setter    def value(self, value):        print('Setting value to ' + value)        self._value = value     # deleting the values    @value.deleter    def value(self):        print('Deleting value')        del self._value  # passing the valuex = Alphabet('Peter')print(x.value) x.value = 'Diesel' del x.value
Output: 
Getting value
Peter
Setting value to Diesel
Deleting value
Using @property decorator works same as property() method.
First, specify that value() method is also an attribute of Alphabet then, we use the attribute value to specify the Python property setter and the deleter. Notice that the same method value() is used with different definitions for defining the getter, setter, and deleter. Whenever we use x.value, it internally calls the appropriate getter, setter, and deleter.
Class Attribute: Class Attributes are unique to each class. Each instance of the class will have this attribute. 
Python3
# declare a classclass Employee:     # class attribute    count = 0     # define a method    def increase(self):        Employee.count += 1 # create an Employee# class objecta1 = Employee() # calling object's methoda1.increase() # print value of class attributeprint(a1.count) a2 = Employee() a2.increase() print(a2.count) print(Employee.count)
Output:
1
2
2
In the above example, the count variable is a class attribute.
Python property(): Returns object of the property class 
Python3
# create a classclass gfg:         # constructor    def __init__(self, value):        self._value = value                 # getting the values    def getter(self):        print('Getting value')        return self._value                 # setting the values    def setter(self, value):        print('Setting value to ' + value)        self._value = value                 # deleting the values    def deleter(self):        print('Deleting value')        del self._value         # create a properties    value = property(getter, setter, deleter, )     # create a gfg class objectx = gfg('Happy Coding!')print(x.value)     x.value = 'Hey Coder!'     # deleting the valuedel x.value
Output:
Getting value
Happy Coding!
Setting value to Hey Coder!
Deleting value
By using property() method, we can modify our class and implement the value constraint without any change required to the client code. So that the implementation is backward compatible.
shubham_singh
kumar_satyam
Python-Functions
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()
Create a Pandas DataFrame from Lists
Python program to convert a list to string
Reading and Writing to text files in Python
*args and **kwargs in Python