Skip to content

Commit 8469373

Browse files
committed
Simplified example
(Caution: UNTESTED!) The example made little sense. The Undo stack was used to run the commands, not just undo them. Renamed. The commands had names, for no reason. They are now added directly to the stack. The stack was edited (popped) while it was being iterated. The cmd variable was unused. Replaced with simple iterator. The stack should be undone in reverse order. reversed() added. The comments had grammatical errors. Corrected.
1 parent 557677b commit 8469373

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

command.py

+10-12
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,17 @@ def undo(self):
1919

2020

2121
if __name__ == "__main__":
22-
undo_stack = []
23-
ren1 = MoveFileCommand('foo.txt', 'bar.txt')
24-
ren2 = MoveFileCommand('bar.txt', 'baz.txt')
22+
command_stack = []
2523

2624
# commands are just pushed into the command stack
27-
for cmd in ren1, ren2:
28-
undo_stack.append(cmd)
25+
command_stack.append(MoveFileCommand('foo.txt', 'bar.txt'))
26+
command_stack.append(MoveFileCommand('bar.txt', 'baz.txt'))
2927

30-
# they can be executed later on will
31-
for cmd in undo_stack:
32-
cmd.execute() # foo.txt is now renamed to baz.txt
3328

34-
# and can also be undone on will
35-
for cmd in undo_stack:
36-
undo_stack.pop().undo() # Now it's bar.txt
37-
undo_stack.pop().undo() # and back to foo.txt
29+
# they can be executed later on
30+
for cmd in command_stack:
31+
cmd.execute()
32+
33+
# and can also be undone at will
34+
for cmd in reversed(command_stack):
35+
cmd.undo()

0 commit comments

Comments
 (0)