Skip to content

Instantly share code, notes, and snippets.

@gustavofonseca
Forked from fabiobatalha/memento.py
Last active December 16, 2015 08:29
Show Gist options
  • Save gustavofonseca/5406327 to your computer and use it in GitHub Desktop.
Save gustavofonseca/5406327 to your computer and use it in GitHub Desktop.
Versão mais enxuta
# 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