AppDividend
Latest Code Tutorials

Python property() Example | property() Function In Python

0

Python property() Example | property() Function In Python is today’s topic. The property() method returns a property attribute. The property() method delivers the property attribute from a given getter, setter, and deleter. If no arguments are given, the property() method returns the base property attribute that doesn’t include any getter, setter, or deleter. If the doc isn’t provided, the property() method takes the docstring of the getter function.

Python property()

The syntax of the Python property() method is following.

property(fget=None, fset=None, fdel=None, doc=None)

The property() method takes four optional parameters:

  1. fget (Optional) – This function for getting the attribute value.
  2. fset (Optional) – This function for setting the attribute value.
  3. fdel (Optional) – This function for deleting the attribute value.
  4. doc (Optional) – The string that contains the documentation (docstring) for the attribute.

See the following example.

# app.py

class Student:
    def __init__(self, fname):
        self._fname = fname

    def getfname(self):
        print('Getting fname')
        return self._fname

    def setfname(self, value):
        print('Setting fname to ' + value)
        self._fname = value

    def delfname(self):
        print('Deleting fname')
        del self._fname

    fname = property(getfname, setfname, delfname, 'fname property')


s = Student('Jane')
print(s.fname)
s.fname = 'Eleven'
del s.fname

See the output.

➜  pyt python3 app.py
Getting fname
Jane
Setting fname to Eleven
Deleting fname
➜  pyt

Here, _fname is used as the private variable for storing the fname of a Student.

We also set:

  1. a getter method getfname() to get the name of the Student,
  2. a setter method setfname() to set the name of the Student,
  3. a deleter method delname() to delete the name of the Student.

Now, we set the new property attribute name by calling the property() method.

As shown in the program, referencing s.fname internally calls getfname() as getter, setfname() as setter and delfname() as deleter through the printed output present inside the methods.

@property Decorator

The @property decorator allows us to define properties easily without calling a property() function manually. Before studying about the @property decorator, let’s understand what is a decorator.

#What is a decorator

In Python, a function is a first-order object. It means that it can be passed as an argument to another function.

It is also possible to define the function inside another function. Such a function is called the nested function. Moreover, the function can return another function.

A decorator is a function that receives another function as an argument. The behavior of an argument function is extended by a decorator without actually modifying it. So, that is it for a decorator.

@decorator Example

Instead of using a property() method, you can use the Python decorator @property to assign a getter, setter, and deleter. See the following code.

# app.py

class Student:
    def __init__(self, fname):
        self._fname = fname

    @property
    def fname(self):
        print('Getting fname')
        return self._fname

    @fname.setter
    def fname(self, value):
        print('Setting fname to ' + value)
        self._fname = value

    @fname.deleter
    def fname(self):
        print('Deleting fname')
        del self._fname

s = Student('Enola')
print('The fname is:', s.fname)

s.fname = 'Millie'

del s.fname

See the output.

➜  pyt python3 app.py
Getting fname
The fname is: Enola
Setting fname to Millie
Deleting fname
➜  pyt

Here, instead of using a property() method, we’ve used the @property decorator.

First, we specify that fname() method is also an attribute of Student. It is done by using the @property before the getter method, as shown in the program.

Next, we use an attribute name to specify the setter and the deleter.

This is done by using @<name-of-attribute>.setter (@fname.setter) for setter method and @<name-of-attribute>.deleter (@fname.deleter) for deleter method.

Notice, we’ve used the same method fname() with different definitions for defining the getter, setter, and deleter.

Now, whenever we use s.fname, it internally calls the appropriate getter, setter, and deleter as shown by the printed output present inside the method.

Finally, Python property() Example | property() Function In Python is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.