Getters and setters in Python

Writing getter and setter methods in Python is seamless using the @property decorator.

Using the @property decorator for getter and setter methods

Directly from the documentation:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class C(object):
    def __init__(self):
        self._x = None

    @property
    def x(self):
        """I'm the 'x' property."""
        print("getter of x called")
        return self._x

    @x.setter
    def x(self, value):
        print("setter of x called")
        self._x = value

    @x.deleter
    def x(self):
        print("deleter of x called")
        del self._x


c = C()
c.x = 'foo'  # setter called
foo = c.x    # getter called
del c.x      # deleter called

An implementation of this method could be a product where a user can set a price after its instantiation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Product:
    def __init__(self, name):
        self._name = name

    @property
    def price(self):
        return self._price

    @price.setter
    def price(self, new_price):
        if new_price < 0 or not isinstance(new_price, float):
            raise Exception("price must be greater than 0 and a float")
        self._price = new_price

    @price.deleter
    def price(self):
        del self._price


p1 = Product("one")
p1.price = 5.5