First, we write a super simple web framework
Event_list = [] #这个event_list中会存放所有要执行的类def run (): For event in event_list: obj = event () #实例化这个类 Obj.execute () #执行类的execute方法class Basehandler (object): "" "The user must inherit the class to standardize methods for all classes, similar to the functionality of the interface The user must define the Execute function in his or her class, otherwise it will error "" " def Execute (self): raise Exception (" You have to override the Execute function ")
Register our own class in this framework, and execute
Import Framclass MyHandler (fram. Basehandler): # Pass def execute (self): print ("event_drive execute MyHandler") fram.event_list.append ( MyHandler) Fram.run () "" "to explain the above code, we use a web framework to explain if we want to register our own events in the Web Framework 1, we have to write a class, this class is to register the Web framework in 2, However, this class must inherit BaseHandler3, the class must write the Execute method, if not written, will execute the Fram in the Execute Method 4, and then register similar to event_list in 5, and then execute Fram the Run method "" "
Python Event-driven small example