You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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:
as seen in the for loop:
I can print all the values of the previous states.
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.
The text was updated successfully, but these errors were encountered: