Python property()

The property() construct returns the property attribute.

The syntax of property() is:

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

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

1. property() Parameters

The property() takes four optional parameters:

  • fget (optional) – Function for getting the attribute value. Defaults to None.
  • fset (optional) – Function for setting the attribute value. Defaults to None.
  • fdel (optional) – Function for deleting the attribute value. Defaults to None.
  • doc (optional) – A string that contains the documentation (docstring) for the attribute. Defaults to None.

2. Return value from property()

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

  • If no arguments are given, property() returns a base property attribute that doesn’t contain any getter, setter or deleter.
  • If doc isn’t provided, property() takes the docstring of the getter function.

3. Example 1: Create attribute with getter, setter, and deleter

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

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

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

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

    # Set property to use get_name, set_name
    # and del_name methods
    name = property(get_name, set_name, del_name, 'Name property')

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

Output

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

We also set:

  • a getter method get_name() to get the name of the person,
  • a setter method set_name() to set the name of the person,
  • a deleter method del_name() 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 get_name() as getter, set_name() as setter and del_name() as deleter through the printed output present inside the methods.

4. Example 2: Using @property decorator

Instead of using property(), 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

Output

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

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

First, we specify that name() method is also an attribute of Person. This 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.setter for the setter method and @name.deleter for the 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.