2019-05-20 | Paul Boddie | raw annotate files changeset graph | Experiment with a temporary attribute on objects. This attribute must be cleared when assignments occur, but it should also be cleared when objects escape from functions (which is not yet done). Since the motivation for marking objects as temporary is their re-use in certain operations, the objective is to avoid objects being used in multiple places and still being marked as temporary and potentially undergoing re-use/mutation. Perhaps the only way of guaranteeing exclusive access to an object is if it is provided as a function result and is immediately passed to a function or operation, with this function either mutating the object or ensuring its direct propagation to another such function. | temporary-object-experiment |
1 class SignalState: 2 Red = 1 3 Amber = 2 4 Green = 3 5 AmberAgain = 4 6 Off = None 7 8 class Signal: 9 def __init__(self): 10 self.disable() 11 12 def enable(self): 13 self.state = SignalState.Red 14 15 def disable(self): 16 self.state = SignalState.Off 17 18 def next(self): 19 if self.state == SignalState.Red: 20 self.state = SignalState.Amber 21 elif self.state == SignalState.Amber: 22 self.state = SignalState.Green 23 elif self.state == SignalState.Green: 24 self.state = SignalState.AmberAgain 25 elif self.state == SignalState.AmberAgain: 26 self.state = SignalState.Red 27 else: 28 self.state = SignalState.Off 29 30 s = Signal() 31 print s.state # None 32 s.next() 33 print s.state # None 34 s.enable() 35 print s.state # 1 36 s.next() 37 print s.state # 2 38 s.next() 39 print s.state # 3 40 s.next() 41 print s.state # 4 42 s.next() 43 print s.state # 1