-
-
Save gustavofonseca/5406327 to your computer and use it in GitHub Desktop.
Versão mais enxuta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import copy | |
class MementoMixin(object): | |
def __init__(self, *args, **kwargs): | |
self._history = [] | |
def commit(self): | |
self._history.append(copy.deepcopy(vars(self))) | |
def rollback(self): | |
self.__dict__ = self._history.pop() | |
class Pessoa(MementoMixin): | |
def __init__(self, nome, idade): | |
super(Pessoa, self).__init__() | |
self.nome = nome | |
self.idade = idade | |
if __name__ == '__main__': | |
gus = Pessoa(u'Gustavo', 29) | |
gus.commit() | |
print(u'O nome é', gus.nome) | |
gus.nome = u'Gus' | |
print(u'Agora é', gus.nome) | |
gus.rollback() | |
print(u'E agora é', gus.nome) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment