Property() Method in Python With Examples

In Python, the property() function is used to define different types of properties in the class. Suppose, there is a student class that has some attributes like name, roll _number, etc. This student class has a setter and getter method. The setter method assigns the value of private attributes and the getter method returns the value of private attributes. This setter and getter methods are really useful when programmers want to access an attribute. Here, property() function provides an interface that is used to encapsulate the private data members or attributes. This process is very much similar to java and C# programming language. This setter, getter, and delete methods are used as arguments and all these methods are passed through property(). The object of property class is returned and the value of the property() is stored in this object. 

Syntax: propery (getMethod, setMethod, delMethod, doc )

Parameters of property() method:

  • getMethod(): This method returns the value of the private attribute.
  • setMethod(): This method is used to set or assign value to attributes.
  • delMethod(): This method is used to delete or remove the values from attributes.
  • doc(): This method is used to return the string documentation.

Return value of property():

This property() returns the value that is taken from the setter, getter, and delete.

  • If the property() does not have any arguments, it returns the base property attribute. 
  • If the docstring is not passing as arguments through the property(), it takes the default string declaration of a getter() function.

Sample code:

# Creation of Student class using setter, getter, and deleter

class Student:
    def __init__(self, name):
                self._name = name

    #getter() method is used to get the values
    def getName(self):
                return self._name

    #setter() method is used to set or assign values to name attribute
    def set_name(self, name):
                self._name = name

    # delete method is used to delete the values of attributes
    def delName(self):
                del self._name

    # creation of property method and passing the arguments 
    prop = property(getName, setName, delName) 

# Passing the values
x = Student(‘Jack’)

Using of @property decorator:

 This decorator works same like the property() method. The @property decorator provides the features to declare and define properties without calling the property(). Using a decorator, another function can be passed as an argument. A user can define their own user-defined decorators to extend the behavior of another function without any kind of modification. The @property decorator is an in-built decorator in Python. 

Syntax:

# for getter method

 @[decorator_function_name]

#for setter method

@[property_name].setterMethod

#for delete method

@[property_name].deleterMethod

Sample code:

#creation Student class using the decorator
class Student:
    def __init__(self, name):
                self._name = name

    # using property decorator declare a function
    # get the values of attributes
    @property
    def name (self):
                return self._name

    # instead setter(), decorator is used to setting the values
    @name.setter
    def  name(self, name):
                self._name = name

    #instead delete(), the decorator is used to deleting the values of an attribute
    @name.deleter
    def name(self):
                del self._name 

    # Passing the values
    x = Student(‘Jack’)

In the above sample code, class Students have the same name() three times but with different numbers of parameters. Here, @property decorator is used before the name(self) function that indicates that this is a getter method. The value of _name is assigned to the name(self, name) which indicates that it is a setter method. For the setter method, @name.setter decorator is used. In the delete method, @name.deleter decorator and del keyword are used. The name of the property is the name of the method only.