In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to apply the memo mode of Python". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "how to apply the memo mode of Python" can help you solve the problem.
Memo mode
Memo mode, without breaking the closure, captures the internal state of an object and saves that state outside the object. This allows you to restore the object to its previously saved state later.
To put it simply, we can record a state during the run and restore the current state when an error is encountered, which is designed to handle exception situations in the business process.
Advantages:
Sometimes the internal information of some initiator object must be saved outside the initiator object, but it must be read by the initiator object itself. At this time, the memo mode can be used to shield the complex internal information of the initiator from other objects, so that the boundary of the encapsulation can be properly maintained.
This model simplifies the sponsors. Sponsors no longer need to manage and save versions of their internal states, and clients can manage the versions of these states they need.
When the state of the initiator role changes, it is possible that the state is invalid, and the state can be restored using a temporarily stored memo.
Disadvantages:
If the state of the initiator role needs to be fully stored in the memo object, the memo object can be expensive in terms of resource consumption.
When the responsible person role stores a memo, the responsible person may not know how much storage space this state will take up, thus unable to remind the user whether an operation is expensive.
When the status of the initiator role changes, it is possible that the protocol is invalid. If the success rate of state change is not high, it is better to adopt the "if" protocol mode.
Application scenario
Entity roles:
Originator (initiator): responsible for creating a Memento (memo) to record its own internal state at the current moment, and can use the memo to restore the internal state. Originator can decide which internal states Memento stores as needed.
Memento (memo): responsible for storing the internal state of Originator objects and preventing objects other than Originator from accessing memos. Memos have two interfaces:
1.Caretaker can only see the narrow interface of the memo, and he can only pass the memo to other objects.
2.Originator, on the other hand, can see the wide interface of the memo, allowing it to access all the data needed to return to the previous state.
Caretaker (manager): responsible for Memento, can not access or operate the content of Memento.
Code example class AddNumber: def _ _ init__ (self): self.start = 1 def add (self, number): self.start + = number print (self.start) class Memento: "memo"def backups (self)" Obj=None): "" set backup method: param obj:: return: "" self.obj_dict = copy.deepcopy (obj.__dict__) print ("backup data: {}" .format (self.obj_dict)) def recovery (self) Obj): "" restore backup method: param obj:: return: "" obj.__dict__.clear () obj.__dict__.update (self.obj_dict) return objif _ _ name__ ='_ main__': test = AddNumber () memento = Memento () for i in [1, 2, 3,'n' 4]: if I = = 2: memento.backups (test) try: test.add (I) except TypeError as e: print (e) print (test.start) memento.recovery (test) print (test.start) this is the end of the introduction to "how to apply Python's memo mode" Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.