Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
LeiYangGH opened this issue May 17, 2023 · 2 comments
Open

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

LeiYangGH opened this issue May 17, 2023 · 2 comments

Comments

@LeiYangGH
Copy link

LeiYangGH commented May 17, 2023

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
@faif
Copy link
Owner

faif commented May 23, 2023

Sure, any simplification is welcome. Feel free to create a pull request

@LeiYangGH
Copy link
Author

created #408. test passed. could you review?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants