Python property() Function with Examples

property() Function in Python:

The property() construct returns the attribute of the property.

Syntax:

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

Parameters

The property() function accepts four optional parameters:

fget (Optional): Function for obtaining the value of an attribute. None is the default value.
fset (Optional): A function for setting the value of an attribute. None is the default value.
fdel (Optional): Deletes the attribute value. doc (optional) – A string containing the documentation (docstring) for the attribute. None is the default value.

doc (Optional): A string containing the attribute’s documentation (docstring). None is the default value.

Return Value:

property() function returns the property attribute based on the getter, setter, and deleter arguments.

  • If no arguments are provided, property() returns a base property attribute with no getter, setter, or deleter.
  • If doc is not provided, property() uses the getter function’s docstring.

property() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Create a class name as an Employee.
  • Inside the class, create a function say getName().
  • Inside the getName() function print some random text like ‘Get the employee Name’.
  • Return the name value of the given class.
  • Create another function say setName() which accepts the given value as an argument.
  • Print the given value.
  • Set the name as the given value.
  • Create another function say delName().
  • Inside the delName() function print some random text.
  • Delete the given name using the del function.
  • Set or configure property to use getName, setName and delName methods
  • Create an object for the given class person by passing some random name as an argument.
  • Print the name of the above object.
  • Modify the name of the above object with some random name.
  • Delete the name of the above object.
  • The Exit of the Program.

Below is the implementation:

# Create a class name as an Employee.
class Employee:
    def __init__(self, ename):
        self.empname = ename

    # Inside the class, create a function say getName().
    def getName(self):
        # Inside the getName() function print some random text like
        # 'Get the employee Name'.
        print('Get the employee Name :')
        # Return the name value of the given class.
        return self.empname
   # Create another function say setName() which accepts the given value as
   # an argument.

    def setName(self, val):
        # Print the given value.
        print('Set the employee Name : ' + val)
        # Set the name as the given value.
        self.empname = val

    # Create another function say delName().
    def delName(self):
        # Inside the delName() function print some random text.
        print('Delete the employee Name ')
        # Delete the given name using the del function.
        del self.empname

    # Set or configure property to use getName, setName
    # and delName methods
    ename = property(getName, setName, delName, 'Name property')


# Create an object for the given class person by passing some random name as
# argument.
e = Employee('Dhoni')
# Print the name of the above object.
print(e.ename)
# Modify the name of the above object with some random name.
e.ename = 'Virat'
# Delete the name of the above object.
del e.ename

Output:

Get the employee Name :
Dhoni
Set the employee Name : Virat
Delete the employee Name

Explanation:

In this case, empname is used as a private variable to store Employee name.

We also established or set:

  • A getter method getName() for obtaining the Employee name.
  • A setter method setName() for setting the Employee name.
  • A deleter method delName() for deleting the Employee name.

By calling the property() method, we can now set a new property attribute name.

Referencing e.ename, as shown in the program, internally calls getName() as a getter, setName() as a setter, and delName() as deleter via the printed output present inside the methods.