Python property() Function

One can programmatically create a property of a class that references the getter, setter, and deleter functions for an attribute of the class, and optionally documentation for the attribute as well.

Creating a property is an advanced technique that is useful when one wants to expose the getter, setter, and deleter "private" functions as a property of the class, which is more readable and gives the programmer of the class more control over what the behavior of the property.

Syntax

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

Parameters

ParameterDescription
fget Optional. The getter function. Default is None
fset Optional. The setter function. Default is None
fdel Optional. The deleter function. Default is None
doc Optional. The docuemation string for this property. Default is None

Example

Python
class Person():
    def __init__(self):
        # _file is a "private" attribute of Person.
        self._file = ''
            
        # The leading underscore _ indicates to another programmer that
        # these are considered "private" functions and should not be
        # called directly.

        def _getName(self):
            return self._name
        
        def _setName(self, value):
            self._name = value
                
        def _delName(self):
            self._name = ''
        
        # Create a property called 'name' using the getter, setter and deleter
        # Bonus points for setting the documentation string
        name = property(_getName, _setName, _delName, doc='Name of the person')
      
# We can now access the getter, setter, and deleter using just "name"
person = Person()
person.name = "Joe"
print(person.name)

Here, we have a private variable

_name</codee> and a collection of methods that
allow us to get and set the name. We also include a "Deleter" method that "deletes" the
name by setting it to blank ('')</p>

<h2>Notes</h2>
<p>The deleter is usually used to delete resources used by a property, such as file handles
or memory that was allocated. You can use the <code>del
keyword to delete the private variable that is holding the value of the property, but this usually isn't done.

If we delete the property, trying to use the property again causes an exception to be

Python
del person.name
print(person.name)
Output:
Traceback (most recent call last):
  File "BuiltInFunctions.py", line 30, in <module>
    print(person.name)
  File "BuiltInFunctions.py", line 10, in _getName
    return self._name
AttributeError: 'Person' object has no attribute '_name'

Example

Python
class Person():
    def __init__(self):
        # _name is a "private" attribute of Person.
        self._name = ''
    
    # The leading underscore _ indicates to another programmer
    # that these are considered "private" functions and should
    # not be callled directly.
    def _getName(self):
        return self._name

    def _setName(self, value):
        self._name = value
        
    def _delName(self):
        del self._name

    # make a property
    name = property(_getName, _setName, _delName, doc='name of the person')

# We can now access the getter, setter, and deleter using just "name"
person = Person()
person.name = "Joe"
print(person.name)

Notes

Usually properties are not deleted. If we delete the property, trying to use the property causes an exception to be thrown. Using the code above, if we try to use a deleted property, we see that Python throws an AttributeError exception.

Example

Python
del person.name
print(person.name)
Output:
Traceback (most recent call last):
  File "BuiltInFunctions.py", line 30, in <module>
    print(person.name)
  File "BuiltInFunctions.py", line 10, in _getName
    return self._name
AttributeError: 'Person' object has no attribute '_name'

Output

Joe