diff --git a/patterns/behavioral/memento.py b/patterns/behavioral/memento.py index 545975d3..c1bc7f0b 100644 --- a/patterns/behavioral/memento.py +++ b/patterns/behavioral/memento.py @@ -41,32 +41,20 @@ def rollback(self): a_state() -class Transactional: +def Transactional(method): """Adds transactional semantics to methods. Methods decorated with + @Transactional will roll back to entry-state upon exceptions. - @Transactional will rollback to entry-state upon exceptions. + :param method: The function to be decorated. """ - - def __init__(self, method): - self.method = method - - def __get__(self, obj, T): - """ - A decorator that makes a function transactional. - - :param method: The function to be decorated. - """ - - def transaction(*args, **kwargs): - state = memento(obj) - try: - return self.method(obj, *args, **kwargs) - except Exception as e: - state() - raise e - - return transaction - + def transaction(obj, *args, **kwargs): + state = memento(obj) + try: + return method(obj, *args, **kwargs) + except Exception as e: + state() + raise e + return transaction class NumObj: def __init__(self, value):