A fool-proof and robust way of dealing with dependent attributes is to use my small pure-python module exactly for handling acyclic dependencies in python class attributes, also known as dataflow programming.
Sometimes it is not feasible to simply update all attributes when one is changed. Ideally, changing an attribute doesn't cause any updates. Only getting an attribute should cause updates, and only the required updates.
That is exactly what the module does. It is very easy to implement in any class definition.
For instance, consider an attribute dependency structure shown below:
This dependency structure is completely defined using the library's descriptors:
# The following defines the directed acyclic computation graph for these attributes.
a1 = IndependentAttr(init_value = 1, name = 'a1')
a2 = DeterminantAttr(dependencies = ['a1'], calc_func = 'update_a2', name = 'a2')
a3 = DeterminantAttr(dependencies = ['a2'], calc_func = 'update_a3', name = 'a3')
a4 = DeterminantAttr(dependencies = ['a1','a2'], calc_func = 'update_a4', name = 'a4')
a5 = DeterminantAttr(dependencies = ['a1','a2','a3','a6'], calc_func = 'update_a5', name = 'a5')
a6 = IndependentAttr(init_value = 6, name = 'a6')
a7 = DeterminantAttr(dependencies = ['a4','a5'], calc_func = 'update_a7', name = 'a7')
# ...... define the update functions update_a2, update_a3 etc
The module takes care of the rest. Changing an attribute causes its children (attributes it affects) (and their children, etc) to know their values need to be recalculated in the future. When the value of an attribute is requested by calling
__get__ (for instance by executing
obj.a5), only the required updates occur, all automatically behind the scenes.
If it doesn't suit your needs, at least it is a helpful example of how the descriptor functionalities work. This example is available at the github page. Descriptors provide lots of flexibility for modifying how
__del__ work for your class attributes, which can solve dependency problems.