I have the following code:
class StringIterator:
def __init__(self, compressedString: str):
self.compressedString = compressedString
self.i = 0
self.idigit = self.i +2
The problem is that the variable 'self.idigit' (linked to self.i) will not update if self.i changes. For example, if I increment self.i with 1 (self.i += 1), printing or returning self.idigit will not recalculate the variable (returning 1+2=3). How can I do that?
@property?self.idigitbe a member. Have it be a function. You shouldn't have "duplicate" state in your object. Have the state hold the base data, use methods to return stuff derived from that data. Otherwise, it's too easy to have the object's state be inconsistent.x = 'D'y = x+'123'There is no way to make y update automatically if x changes?