Python Properties

Some object-oriented languages such as Java and C# support private object attributes; which cannot be directly accessed from outside. Programmers often have to write getter and setter methods to access such private attributes.

However in Python, all the attributes and methods are public, so it is useless to write getters or setters.

If you want to prevent direct access to an attribute, you should define it as a property.

It is a simple way to customize access to an attribute.

Define a Property

Let’s see how properties can be defined in Python. Here is a simple example in which the Person class is defined.

This class has a single attribute named hidden_name which we do not want people to access directly. Hence, two methods are defined in the class – a getter called get_name() and a setter called set_name().

Example:

class Person():
    def __init__(self, value):
        self.hidden_name = value

    # getter function
    def get_name(self):
        print('Getting name:')
        return self.hidden_name
    
    # setter function
    def set_name(self, value):
        print('Setting name to', value)
        self.hidden_name = value
    
    # make a property
    name = property(get_name, set_name)

The get_name() and set_name() methods act like normal getter and setter until this line.

name = property(get_name, set_name)

It creates a new class attribute called ‘name’ and defines the two methods as properties.

Now when you refer to the name attribute of any Person object, Python actually calls the get_name() method.

Example:

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

Output:

Getting name:
Bob

When you assign a value to the name attribute, the set_name() method is called.

Example:

p.name = "Sam"
print(p.name)

Output:

Setting name to Sam
Getting name:
Sam

A great feature of a property is that it looks like a normal attribute, but when you access it, it automatically triggers the getter and setter methods.

property() Function

You generate a property by calling the property() built-in function, passing in three methods (getter, setter and deleter) as well as the docstring for the property.

The property() function has the following syntax:

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

This creates a class attribute called attrib and defines the three methods as properties.

Now, when you reference x.attrib, Python calls the fget method.

When you assign x.attrib = value, Python calls the fset method and passes value as an argument.

When you execute del x.attrib, Python calls the fdel method.

Python uses the argument you passed as doc as the docstring of the attribute.

Rewriting Person Class

Let’s rewrite our previous example, including the deleter method and docstring.

Example:

class Person():
    def __init__(self, value):
        self.hidden_name = value
    
    # getter function
    def get_name(self):
        print('Getting name:')
        return self.hidden_name

    # setter function
    def set_name(self, value):
        print('Setting name to', value)
        self.hidden_name = value
        
    # deleter function
    def del_name(self):
        print('Deleting name')
        del self.hidden_name

    # make a property
    name = property(get_name, set_name, del_name, doc='name of the person')

Here is an example of the new class in use:

Example:

p = Person('Bob')

# calls the getter
print(p.name)

# calls the setter
p.name = 'Sam'

# prints docstring
print('Docstring:', Person.name.__doc__)

# calls the deleter
del p.name

Output:

Getting name:
Bob
Setting name to Sam
Docstring: name of the person
Deleting name

@property – Property as a Decorator

A more elegant syntax to define properties in a class is to use property as a decorator

In the next example, we’ll define three different methods, each called name() but preceded by different decorators:

  • @property decorator goes before the getter method
  • @name.setter decorator goes before the setter method
  • @name.deleter decorator goes before the deleter method

Here’s how they actually look in the code:

Example:

class Person():
    def __init__(self, value):
        self.hidden_name = value
    
    @property
    def name(self):
        print('Getting name:')
        return self.hidden_name

    @name.setter
    def name(self, value):
        print('Setting name to', value)
        self.hidden_name = value
        
    @name.deleter
    def name(self):
        print('Deleting name')
        del self.hidden_name

Here the first method is a getter, and establishes name as being a property. The other two methods attach setter and deleter to the name property.

You can still access name as if it were an attribute:

Example:

p = Person('Bob')

# calls the getter
print(p.name)

# calls the setter
p.name = 'Sam'

# calls the deleter
del p.name

Output:

Getting name:
Bob
Setting name to Sam
Deleting name

Please note that you cannot define @name.setter and @name.deleter decorators unless you already establish name as a property using @property decorator.

Real World Example

Properties are generally used in cases where you want to add extra processing (e.g., type checking or validation) to the getting or setting of an instance attribute.

For example, below code defines a property that adds simple type checking to an attribute:

Example:

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

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, value):
        if not isinstance(value, str):
            raise TypeError('Expected a string')
        self._name = value

    @name.deleter
    def name(self):
        raise AttributeError("Can't delete attribute")

p = Person(42)    # TypeError: Expected a string

p = Person('Bob')

print(p.name)     # Bob

p.name = 42       # TypeError: Expected a string

del p.name        # AttributeError: Can't delete attribute

In above example,

1. Type checking is performed in the setter function. It checks whether the type of the assigned value is a string. If it is other than string, the TypeError exception is raised.

Example:

p.name = 42       # TypeError: Expected a string

2. AttributeError exception is raised when user tries to delete an attribute.

Example:

del p.name        # AttributeError: Can't delete attribute

3. Type checking is performed during initialization as well.

Example:

p = Person(42)    # TypeError: Expected a string 

The whole point of our example is to apply type checking when setting an attribute which means that checking should also be done during initialization.

That’s why the __init__() method sets self.name instead of self._name. By setting self name, the __init__() method automatically calls the setter method.

Computed Attributes

Properties can also be a way to define computed attributes – attributes that are not actually stored, but are calculated dynamically on demand.

Let’s define a Rectangle class that has two normal attributes (width and height) and one computed attribute (area)

Example:

class Rectangle(object):
    def __init__(self, width, height):
        self.width = width
        self.height = height

    @property
    def area(self):
        return self.width * self.height

Let’s create a Rectangle object with an initial value for its width and height.

Example:

r = Rectangle(2, 5)

Now you can call the area as if it were an attribute:

Example:

print(r.area)       # 10

Here’s the fun part: you can change the width and height of the rectangle at any time, and the area property will be computed accordingly:

Example:

r.width = 3
r.height = 6

print(r.area)       # 18

As you can see we have not specified setter property for an attribute, we can’t set it from the outside. This is handy for read-only attributes:

Example:

r.area = 18     # AttributeError: can't set attribute

Extending a Property in a Subclass

Because a property is not a single method but a collection of getter, setter, and deleter methods, extending it in a subclass introduces many problems.

You need to figure out whether you will redefine all of the methods together or just one of the methods.

Here is an example of a class that inherits from Person and extends the name property with new functionality:

Example:

class Person():
    def __init__(self, value):
        self.hidden_name = value
    
    @property
    def name(self):
        print('Getting name:')
        return self.hidden_name

    @name.setter
    def name(self, value):
        print('Setting name to', value)
        self.hidden_name = value
        
    @name.deleter
    def name(self):
        print('Deleting name')
        del self.hidden_name

class SubPerson(Person):
    @property
    def name(self):
        print('Inside subperson getter')
        return super().name

    @name.setter
    def name(self, value):
        print('Inside subperson setter')
        super(SubPerson, SubPerson).name.__set__(self, value)

    @name.deleter
    def name(self):
        print('Inside subperson deleter')
        super(SubPerson, SubPerson).name.__delete__(self)

Here is an example of the new class in use:

Example:

s = SubPerson('Bob')

# calls the getter
print(s.name)

# calls the setter
s.name = 'Sam'

# calls the deleter
del s.name

Output:

Inside subperson getter
Getting name:
Bob
Inside subperson setter
Setting name to Sam
Inside subperson deleter
Deleting name

In above example, all of the property methods are redefined together. Within each method, super() function is used to call the superclass’s implementation.

If you only want to redefine one of the methods, it’s not enough to use @property by itself, use code such as the following:

Example:

class SubPerson(Person):
    @Person.name.getter
    def name(self):
        print('Inside subperson getter')
        return super().name

When you do this, only the getter method is replaced and the remaining methods of the property are copied.

If you just want to redefine the setter, use this code:

Example:

class SubPerson(Person):
    @Person.name.setter
    def name(self, value):
        print('Inside subperson setter')
        super(SubPerson, SubPerson).name.__set__(self, value)