Given a class definition that allows 3 possible inputs:
class FooBar(object):
def __init__(self, x=None, y=None, z=None):
if x is not None:
self.x = x
elif if y is not None:
self.y = y
elif if z is not None:
self.z = z
else:
raise ValueError('Please supply either x,y or z')
This 3 inputs are related each other, lets say:
x = .5*y = .25*z
This also implies:
y = .5*z = 2*x
and
z = 2*y = 4*x
When creating a instance of FooBar(), the user need to supply one of those and the __init__ takes care of it.
Now I would like to do the following
- If any one of the 3 variables are changed the others change following the relationship.
To try to accomplish that I did:
@property
def x(self):
return self._x
@x.setter
def x(self, value):
self._x = value
self._y = 2*self._x
self._z = 4*self._x
And to the others:
@property
def y(self):
return self._y
@y.setter
def y(self, value):
self._y = value
self._x = .5*self._y
self._z = 2*self._y
@property
def z(self):
return self._z
@z.setter
def z(self, value):
self._z = value
self._x = .25*self._z
self._y = .5*self._z
Is this the correct approach?
FooBar(x=1, y=1425)?x = 2*y, thenself._y = value/2for the setter ofx. Notvalue * 2.__init__,xtakes precedence, that is the reason to be andif elif....