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

Restricted _state - Memento Design Pattern in Python #8

Open
JuanPabloArbelaez opened this issue Jan 18, 2021 · 1 comment
Open

Restricted _state - Memento Design Pattern in Python #8

JuanPabloArbelaez opened this issue Jan 18, 2021 · 1 comment

Comments

@JuanPabloArbelaez
Copy link

Hello.

I am learning design patterns through the implementations in Python.

The Memento design pattern is supposed to restore previous states, while keeping the information about them, restricted to other objects.

However when implementing the code, I was able to get access to all the restricted states:

if __name__ == "__main__":
    originator = Originator("Super-duper-super-puper-super.")
    caretaker = Caretaker(originator)

    caretaker.backup()
    originator.do_something()
    
    ### this should not work in theory
    for m in caretaker._mementos:
          print(f"_state = {m.get_state()}")  

as seen in the for loop:

I can print all the values of the previous states.

_state = Super-duper-super-puper-super.
_state = kTjoIhPwiBmqvJfuDENWZXKcdUMHtp

is there a way to actually hide these states in Python?

Thanks for your work teaching design patterns. I hope you can guide me on this subject.

@JakubDotPy
Copy link

No, Python doesn't have anything "strictly" restricted. The leading underscore is only an indication that the variable is an implementation detail and should not be used or changed.
In Python everything is accessible.

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

No branches or pull requests

2 participants