Python装饰器入门笔记(2)

def init_data_listener(self):
        for listener_name, listener in inspect.getmembers(self, lambda f: hasattr(f, 'events')):
            for event in listener.events:
                event += listener

@abc.abstractmethod
    def destroy(self):
        raise NotImplementedError

以下为测试代码
class TestEntiy(ManagerBase):

def __init__(self):
        super(TestEntiy, self).__init__()

@msg_listener(game_msg_recv)
    def update(self, protocol):
        print "this is deal data:", protocol

def destroy(self):
        MsgNotifierEvent.clear("game_msg_recv")
       

test = TestEntiy()

game_msg_recv("i am test1")
test.destroy()
game_msg_recv("i am test2")

#输出
#this is deal data: i am test1

Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/9c583a548e61b911e53617b1d02094cf.html