Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to define classes in web memo mode

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Most people don't understand the knowledge points of this article "how to define classes in web memo mode", so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and they have certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article "how to define classes in web memo mode".

sample code

Since Python does not check type information, there is no need to define explicit memo classes. The description of the sample code starts with the originator and goes as follows:

class Originator: def __init__(self): self.a= 0 self.b= 0 self.c= 0 defprint(self): print('a=', self.a, ',b=', self.b,',c=', self.c) def set_memento(self, m): self.__ dict__ = m def create_memento(self): returncopy.copy(self.__ dict__)

In the sample code we define an originator class with three data members and a print method for outputting the state of the object. The other is the create_memento method for generating memos and the set_memento method for restoring memos.

The create_memento method for generating memos generates a copy of the class member dictionary and returns it, while the set_memento method for recovering memos simply replaces its own member dictionary with memo data. It should be added that create_memento must take a copy because it is the only way to ensure that it gets a copy of the object state every time. Next is the test code:

if __name__ == "__main__": c = [] o = Originator() o.print() c.append(o.create_memento()) o.a = 1 o.print() c.append(o.create_memento()) o.b = 2 o.print() c.append(o.create_memento()) o.c = 3 o.print() print('start undo') o.print() while len(c): o.set_memento(c.pop()) o.print()

After creating the originator object, modify the values of the three data members separately. Memos are retrieved prior to each modification and stored in a list, which acts as an accountability class. After three operations, the memos are removed from the list in the reverse order and handed to the originator to restore their status. The results are as follows:

a= 0 ,b= 0 ,c= 0a= 1 ,b= 0 ,c= 0a= 1 ,b= 2 ,c= 0a= 1 ,b= 2 ,c= 3start undoa= 1 ,b= 2 ,c= 3a= 1 ,b= 2 ,c= 0a= 1 ,b= 0 ,c= 0a= 0 ,b= 0 ,c= 0

It can be seen that the data saved in the memo perfectly restored the generator to its pre-operation state.

The above is about the content of this article on "how to define classes in web memo mode". I believe everyone has a certain understanding. I hope the content shared by Xiaobian will be helpful to everyone. If you want to know more relevant knowledge content, please pay attention to the industry information channel.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report