Having a class like
class C():
def __init__(self, bits: list):
self.bits = bits
self.values = [bit.value for bit in self.bits]
c = C(bits)
If I update c.bits
, c.values
wont change. How can I achieve that also c.values
is updatet?
CodePudding user response:
If values is supposed to be dependant on the value of bits at all times, don't store it as a separate object, instead make a function that will return current values. If you use @property decorator, you can even use it like a normal attribute in the code.
class C():
def __init__(self, bits: list):
self.bits = bits
@property
def values(self):
return[bit.value for bit in self.bits]
c = C(bits)
print(c.values)