I am trying to write a simulation class that can easily be extended. For this I'd like to use something similar to a property, but that also provides an update
method that could be implemented differently for different use cases:
class Quantity(object):
def __init__(self, initval=None):
self.value = initval
def __get__(self, instance, owner):
return self.value
def __set__(self, instance, value):
self.value = value
def update(self, parent):
"""here the quantity should be updated using also values from
MySimulation, e.g. adding `MySimulation.increment`, but I don't
know how to link to the parent simulation."""
class MySimulation(object):
"this default simulation has only density"
density = Quantity()
increment = 1
def __init__(self, value):
self.density = value
def update(self):
"""this one does not work because self.density returns value
which is a numpy array in the example and thus we cannot access
the update method"""
self.density.update(self)
The default simulation could the be used like this:
sim = MySimulation(np.arange(5))
# we can get the values like this
print(sim.density)
> [0, 1, 2, 3, 4]
# we can call update and all quantities should update
sim.update() # <- this one is not possible
I would like to write it in such a way such that the simulation can be extended in any user-defined way, for example adding another quantity that is updated differently:
class Temperature(Quantity):
def update(self, parent):
"here we define how to update a temperature"
class MySimulation2(MySimulation):
"an improved simulation that also evolves temperature"
temperature = Temperature()
def __init__(self, density_value, temperature_value):
super().__init__(density_value)
self.temperature = temperature_value
def update(self):
self.density.update(self)
self.temperature.update(self)
Is that possible somehow or is there another way to achieve a similar behavior? I have seen this question, which might help, but the answers seem quite inelegant - is there a good object-oriented approach for my case?
There is a way to achieve a similar behavior.
Step 1: Set a flag on
instance
/MySimulation
.Step 2: Check the flag and return
self
inQuantity.__get__
if the flag is set.Naive implementation(s)
4 lines change.
Note that this is quite intrusive to
MySimulation
and its subclasses.One way to mitigate this is to define an
_update
method for subclasses to override:More robust implementation
Using a metaclass, we can do with 3 lines change to the original code.