Complete the code to define the Memento class that stores the state.
class Memento: def __init__(self, [1]): self._state = state
The Memento class stores the state passed during initialization.
Complete the code to restore the state from the Memento object.
class Originator: def restore(self, memento): self._state = memento.[1]
The Originator accesses the stored state directly from the Memento's _state attribute.
Fix the error in saving the current state into a Memento object.
class Originator: def save(self): return Memento([1])
The Originator saves its current private state attribute into a new Memento.
Fill both blanks to implement the Caretaker storing and retrieving mementos.
class Caretaker: def __init__(self): self._mementos = [] def add_memento(self, memento): self._mementos.[1](memento) def get_memento(self, index): return self._mementos[[2]]
The Caretaker appends mementos to a list and retrieves them by index.
Fill all three blanks to complete the Originator's state change and memento management.
class Originator: def __init__(self): self._state = None def set_state(self, state): self._state = [1] def save(self): return Memento(self._state) def restore(self, memento): self._state = memento.[2] def get_state(self): return self._state originator = Originator() originator.set_state('State1') memento = originator.save() originator.set_state('State2') originator.restore(memento) print(originator.[3]())
The Originator sets its state from the parameter, restores from the Memento's _state attribute, and uses get_state() to retrieve the current state.
