简体   繁体   中英

Python running program backwards

I was wondering if a console-based program in python can run backwards. More specifically, while the program runs, if there is a possibility to undo an action from the user input (eg a user accidentally typed sth incorrect and he wants to go back without restarting the program, like tapping Ctrl+Z to undo) Is that possible? If yes how? :)

No.

Unfortunately Python doesn't provide this feature.

Python is an imperative language with mutable state and implementing an "undo" feature would require a quite heavy refactoring of the language internals (after the user types in the wrong input a lot of things can happen in memory... and all of them would have to be in a "transaction" to allow going back to the previous state).

Consider also that when processing the wrong input a program could for example change a file on disk or send a network message to another computer... how do you "undo" these actions?

I doubt that any language has built in support for rolling back arbitrarily in a program. When you see a program with an undo feature it's because that was implemented manually in terms of the specific state that the program manages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM