How to create Undo function in Python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to create Undo function in Python?

Since I've started with Python 3, I'd like to know is there a way to create an Undo function and how? It could be simple for some calculations to write a reverse function and once it is called, it will undo the last action. Building a list of results or saving a LOT of variables could be possible, but it's not what I'm looking for. Especially when compared to apps with 50 or so Undo steps... So, guys, if you have any ideas, I'd be very grateful! Thanks!

10th Aug 2020, 7:20 AM
Bojan Adžić
Bojan Adžić - avatar
2 Answers
+ 5
If I understand correctly, you should use a stack data structure for undo and redo and just pop back and forth when undoing and redoing. if a new action is done, empty the redo stack and push the new action to the top of thr undo stack.
10th Aug 2020, 7:28 AM
Brian R
Brian R - avatar
+ 2
Brian R Thanks! I wasn't thinking that way... I tried complicating it, but it's much simpler. Thanks again!
10th Aug 2020, 7:47 AM
Bojan Adžić
Bojan Adžić - avatar