Python property()

The property() method a returns a property attribute.

The syntax of property() method is:

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

Recommended Reading: Python @property: How to Use it and Why?


property() Parameters

The property() method takes four optional parameters:

  • fget (Optional) - function for getting the attribute value
  • fset (Optional) - function for setting the attribute value
  • fdel (Optional) - function for deleting the attribute value
  • doc (Optional) - string that contains the documentation (docstring) for the attribute

Return value from property()

The property() method returns a property attribute from the given getter, setter and deleter.

  • 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.

Example 1: Create attribute with getter, setter and deleter using property()

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

    def getName(self):
        print('Getting name')
        return self._name

    def setName(self, value):
        print('Setting name to ' + value)
        self._name = value

    def delName(self):
        print('Deleting name')
        del self._name

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


p = Person('Adam')
print(p.name)

p.name = 'John'

del p.name

When you run the program, the output will be:

Getting name
The name is: Adam
Setting name to John
Deleting name

Here, _name is used as the private variable for storing the name of a Person.

We also set:

  • a getter method getName() to get the name of the person,
  • a setter method setName() to set the name of the person,
  • a deleter method delName() to delete the name of the person.

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

As shown in the program, referencing p.name internally calls getName() as getter, setName() as setter and delName() as deleter through the printed output present inside the methods.

We also set the docstring of the attribute to 'Name property'.


Example 2: Create attribute with getter, setter and deleter using @property decorator

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

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

    @property
    def name(self):
        print('Getting name')
        return self._name

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

    @name.deleter
    def name(self):
        print('Deleting name')
        del self._name

p = Person('Adam')
print('The name is:', p.name)

p.name = 'John'

del p.name

When you run the program, the output will be:

Getting name
The name is: Adam
Setting name to John
Deleting name

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

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

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

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

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

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