2019-05-20 | Paul Boddie | file changeset files shortlog | 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 |
paul@792 | 1 | class SignalState: |
paul@792 | 2 | Red = 1 |
paul@792 | 3 | Amber = 2 |
paul@792 | 4 | Green = 3 |
paul@792 | 5 | AmberAgain = 4 |
paul@792 | 6 | Off = None |
paul@792 | 7 | |
paul@792 | 8 | class Signal: |
paul@792 | 9 | def __init__(self): |
paul@792 | 10 | self.disable() |
paul@792 | 11 | |
paul@792 | 12 | def enable(self): |
paul@792 | 13 | self.state = SignalState.Red |
paul@792 | 14 | |
paul@792 | 15 | def disable(self): |
paul@792 | 16 | self.state = SignalState.Off |
paul@792 | 17 | |
paul@792 | 18 | def next(self): |
paul@792 | 19 | if self.state == SignalState.Red: |
paul@792 | 20 | self.state = SignalState.Amber |
paul@792 | 21 | elif self.state == SignalState.Amber: |
paul@792 | 22 | self.state = SignalState.Green |
paul@792 | 23 | elif self.state == SignalState.Green: |
paul@792 | 24 | self.state = SignalState.AmberAgain |
paul@792 | 25 | elif self.state == SignalState.AmberAgain: |
paul@792 | 26 | self.state = SignalState.Red |
paul@792 | 27 | else: |
paul@792 | 28 | self.state = SignalState.Off |
paul@792 | 29 | |
paul@792 | 30 | s = Signal() |
paul@792 | 31 | print s.state # None |
paul@792 | 32 | s.next() |
paul@792 | 33 | print s.state # None |
paul@792 | 34 | s.enable() |
paul@792 | 35 | print s.state # 1 |
paul@792 | 36 | s.next() |
paul@792 | 37 | print s.state # 2 |
paul@792 | 38 | s.next() |
paul@792 | 39 | print s.state # 3 |
paul@792 | 40 | s.next() |
paul@792 | 41 | print s.state # 4 |
paul@792 | 42 | s.next() |
paul@792 | 43 | print s.state # 1 |