@property decorator

https://docs.python.org/3/library/functions.html

INTRODUCTION

@property helps to manipulate attributes.


# First approach.

class Engine:

    def __init__(self):
        self.temperature = 0

e = Engine()
assert e.temperature == 0   # like getT() in C++
e.temperature = 100         # like setT(value) in C++
assert e.temperature == 100
del e.temperature
#print(e.temperature)   # AttributeError

# Second approach with the extended usability.
# The interface of the Engine class is not changed!

#class Engine(object):   # Py2
class Engine:         # Py3

    def __init__(self):
        self._t = 0   # the private variable

    @property
    def temperature(self):
        """I'm the 'temperature' property."""
        return self._t

    @temperature.setter
    def temperature(self, value):
        if value < 0:
            raise ValueError("temperature less then zero")
        self._t = value

    @temperature.deleter
    def temperature(self):
        del self._t   # better self._t = 0 (?)


e = Engine()
assert e.temperature == 0
e.temperature = 100
assert e.temperature == 100
#e.temperature = -1   # ValueError: temperature less then zero
del e.temperature
#print(e.temperature)   # AttributeError: 'Engine' object has no attribute '_t'