Skip to content

memento example, why Transactional is defined as a descriptor? #407

@LeiYangGH

Description

@LeiYangGH

learning the example patterns/behavioral/memento.py.
Transactional is defined as a descriptor, this looks more complicated

class Transactional:
    def __init__(self, method):
        self.method = method
    def __get__(self, obj, T):
        def transaction(*args, **kwargs):
            state = memento(obj)
            try:
                return self.method(obj, *args, **kwargs)
            except Exception as e:
                state()
                raise e
        return transaction

how about refactor to normal higher-order function?

 def Transactional(method):
    def transaction(obj, *args, **kwargs):
        state = memento(obj)
        try:
            return method(obj, *args, **kwargs)
        except Exception as e:
            state()
            raise e
    return transaction

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions