Python property() function
Python property() function returns the object of the property class and it is used to create the property of a class. In Python, the property() function is a built-in function that allows us to create a property for a class. In this article, we will learn more about the Python property() function.
Python property() Function Syntax
Python property() function is used to create a class property in Python.
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.
What is property() Function in Python
Python property() function is a built-in function that allows us to create a special type of attribute called a property for a class. Properties are used to encapsulate the access to an object attribute and to add some logic to the process such as computation, access control, or validation.
Python property() Function Methods
Below are the ways by which we can create property for a class in Python:
- Using property() method
- Using @property decorator
Using property() Method
In this example, we are using the property()
function to create a class property in Python. We define a class called Alphabet
, and within this class, we create a property named value
to encapsulate access to an internal attribute _value
. This property allows us to control how the _value
attribute is accessed and modified by providing custom getter and setter methods.
Python3
class Alphabet:
def __init__( self , value):
self ._value = value
def getValue( self ):
print ( 'Getting value' )
return self ._value
def setValue( self , value):
print ( 'Setting value to ' + value)
self ._value = value
def delValue( self ):
print ( 'Deleting value' )
del self ._value
value = property (getValue, setValue,
delValue, )
x = Alphabet( 'GeeksforGeeks' )
print (x.value)
x.value = 'GfG'
del x.value
|
Output
Getting value
GeeksforGeeks
Setting value to GfG
Deleting value
Using @property Decorator
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. 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.
Python3
class Alphabet:
def __init__( self , value):
self ._value = value
@property
def value( self ):
print ( 'Getting value' )
return self ._value
@value .setter
def value( self , value):
print ( 'Setting value to ' + value)
self ._value = value
@value .deleter
def value( self ):
print ( 'Deleting value' )
del self ._value
x = Alphabet( 'Peter' )
print (x.value)
x.value = 'Diesel'
del x.value
|
Output
Getting value
Peter
Setting value to Diesel
Deleting value
Python Property vs Attribute
Class Attribute: Class Attributes are unique to each class. Each instance of the class will have this attribute. In the given example, the count variable is a class attribute.
Python3
class Employee:
count = 0
def increase( self ):
Employee.count + = 1
a1 = Employee()
a1.increase()
print (a1.count)
a2 = Employee()
a2.increase()
print (a2.count)
print (Employee.count)
|
Python property(): Returns object of the property class. In this example, we are demonstrating the use of property() function.
Python3
class gfg:
def __init__( self , value):
self ._value = value
def getter( self ):
print ( 'Getting value' )
return self ._value
def setter( self , value):
print ( 'Setting value to ' + value)
self ._value = value
def deleter( self ):
print ( 'Deleting value' )
del self ._value
value = property (getter, setter, deleter, )
x = gfg( 'Happy Coding!' )
print (x.value)
x.value = 'Hey Coder!'
del x.value
|
Output
Getting value
Happy Coding!
Setting value to Hey Coder!
Deleting value
Applications
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.
Last Updated :
29 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...