Python property() Function – Tutorial with Examples

The property() function is a built-in function in Python that returns a property attribute. In Python, a property is a special type of attribute that is accessed like a field but it is computed by calling a getter method. It is a way to define getter, setter, and deleter methods for an object’s attributes without having to access them directly. The property() function is used to create these special attributes, which can be used to define getter, setter, and deleter methods for class attributes.

Syntax

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

Parameters

  • fget (function): A function used to get the value of the attribute.
  • fset (function): A function used to set the value of the attribute.
  • fdel (function): A function used to delete the value of the attribute.
  • doc (string): A string used to document the attribute.

Return Value

The property() function returns a property attribute, which is a special type of attribute that can be used to define getter, setter, and deleter methods for class attributes.

Examples

Example 1: Using property() to create a getter method

In this example, we will create a class named Person, which has a name attribute. We will use the property() function to create a getter method for the name attribute. The getter method will return the value of the name attribute.

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

    def get_name(self):
        return self._name

    name = property(get_name)

person = Person("John Doe")
print(person.name) # Output: John Doe

Example 2: Using property() to create a setter method

In this example, we will create a class named Person, which has a name attribute. We will use the property() function to create a setter method for the name attribute. The setter method will set the value of the name attribute.

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

    def get_name(self):
        return self._name

    def set_name(self, name):
        self._name = name

    name = property(get_name, set_name)

person = Person("John Doe")
print(person.name) # Output: John Doe
person.name = "Jane Doe"
print(person.name) # Output: Jane Doe

Example 3: Using property() to create a deleter method

In this example, we will create a class named Person, which has a name attribute. We will use the property() function to create a deleter method for the name attribute. The deleter method will delete the value of the name attribute.

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

def get_name(self):
    return self._name

def set_name(self, name):
    self._name = name

def del_name(self):
    del self._name

name = property(get_name, set_name, del_name)
person = Person("John Doe")
print(person.name) # Output: John Doe
del person.name
try:
    print(person.name)
except AttributeError as e:
    print(e) # Output: 'Person' object has no attribute '_name'

Example 4: Using property() with the doc parameter

In this example, we will create a class named Person, which has a name attribute. We will use the property() function to create a getter method for the name attribute and provide a documentation string for the attribute using the doc parameter.

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

def get_name(self):
    """Get the name of the person."""
    return self._name

name = property(get_name, doc="The name of the person.")
person = Person("John Doe")
print(person.name) # Output: John Doe
print(Person.name.doc) # Output: The name of the person.

Conclusion

The property() function is a useful tool in Python for creating special attributes, known as properties, that can be used to define getter, setter, and deleter methods for class attributes. Properties allow you to access an attribute as if it were a field, but with the added benefit of allowing you to define methods for getting, setting, and deleting the attribute’s value. The property() function can be used in conjunction with the @property and @attribute_name.setter decorators to make the code more concise and readable.

Leave a Reply

Your email address will not be published. Required fields are marked *