Это классический случай Command Pattern. Ниже приведен пример реализации функции отмены в Python:
from os import rename
class RenameFileCommand(object):
def __init__(self, src_file, target_file):
self.src_file=src_file
self.target_file=target_file
def execute(self):
rename(self.src_file, self.target_file)
def undo(self):
rename(self.target_file,self.src_file)
class History(object):
def __init__(self):
self.commands=list()
def execute(self, command):
command.execute()
self.commands.append(command)
def undo(self):
self.commands.pop().undo()
if __name__=='__main__':
hist=History()
hist.execute(RenameFileCommand( 'test1.txt', 'tmp.txt', ))
hist.undo()
hist.execute(RenameFileCommand( 'tmp2.txt', 'test2.txt',))