Consider the following minimal example for my question:
class MyClass:
a = False
b = 0
if a:
b = 1
MCinst = MyClass()
Here MCinst
has two attributes, MCinst.a
and MCinst.b
with default values False
and 0
respectively. What I was trying to achieve with the if
statement, is that the value of MCinst.b
would automatically switch to 1
when I set MCinst.a = True
. But this obviously does not work as intended, since the value of MCinst.b
stays 0
.
I know that I could remove the if
statement and simply modify b
from the outside, in the same way in which I modified a
. But I am still curious if there is a way to make work what I wanted—i.e. to have an attribute of an instance change automatically when I change another attribute of the same instance.