I have this python problem (simplified here for the sake of the example) where I have 2 classes: One child class (to be injected) and one parent class. In the parent class, I have an attribute that stores an instance of the child class (via dependency injection). This attribute has a getter and a setter.
What I am trying to achieve is have something happen when an (any) attribute in the child instance changes (outside the parent class). However, in the below example, nothing happens. The only way to trigger the setter is if I replace the child class with another instance (which I don't want).
class Spline3D:
def __init__(self, num=20):
self.num = num
class Spline3DViewer:
def __init__(self, spline3D):
self._spline3D = spline3D
@property
def spline3D(self):
return self._spline3D
@spline3D.setter
def spline3D(self, value):
self._spline3D = value
print(f"Updated self.spline3D.num is = {self._spline3D.num}")
spline3D = Spline3D(num=30)
spline3DViewer = Spline3DVTK(spline3D=spline3D)
spline3D.num = 40 ### This should, technically, trigger the @spline3D.setter in Spline3DViewer
and print "Updated self.spline3D.num is = 40" but nothing happens
However, this works (not the solution I want):
spline3D2 = Spline3D(num=40)
spline3DViewer.spline3D = spline3D2
I'm clearly doing something wrong but I can't seem to find the solution. Any help is appreciated.
Kind regards,
You say that
But it should not. The setter is only called when you assign a new value to the spline3D field of spline3DViewer, not when you change a field of spline3D. So the observed behavior is correct.