property() Built-in Function

Python – property()

Python property() built-in function returns a property with specific setter function, getter function, and delete function.

In this tutorial, we will learn the syntax and usage of property() built-in function with examples.


The syntax of property() function is

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


fgetOptionalA function for getting the attribute value.
fsetOptionalA function for setting the attribute value.
fdelOptionalA function for deleting the attribute value.
docOptionalThe doc string for the attribute.


1. property() in a Class definition

In the following program, we define a class named Student. This class has two attributes: name, and marks.

We use property() function to assign the property marks in the Student class.

Python Program

class Student:
    def __init__(self):
        self._points = None

    def getPoints(self):
        return self._points

    def setPoints(self, value):
        self._points = value

    def delPoints(self):
        del self._points

    name = ""
    points = property(getPoints, setPoints, delPoints, "Total points scored by student for a total of 100.")

student1 = Student() = "Ram"

student1.points = 65 #setPoints() will be called
print(student1.points) #getPoints() will be called
del student1.points #delPoints() will be called
Run Code Online




In this Built-in Functions tutorial, we learned the syntax of the property() built-in function, and how to use this function to create a property with specific setter, getter, and delete function.

⫷ Previous tutorialNext tutorial ⫸