How to do it...

We'll break this into two parts. First, the general overview of defining settable properties, then the details of how to track state changes:

  1. Define a class with a meaningful name.
  1. Provide hidden attributes. These will be exposed as properties:

        class Leg: 
        def __init__(self): 
            self._rate= rate 
            self._time= time 
            self._distance= distance. 
  1. For each gettable property, provide a method to compute the property value. In many cases, these will parallel the hidden attributes:

        @property 
        def rate(self): 
            return self._rate 
  1. For each settable property, provide a method to set the property value:

        @rate.setter 
        def rate(self, value): 
            self._rate = value 
            self._calculate('rate')
 

The setter method has a special property decorator ...

Get Modern Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.