装饰器能不能这样用?



 class Event(object):
    def __init__(self):
        self.m=[]
    def __call__(self,hand):
        def hand_w(hand):
            return self.__call__(hand)
            return hand_w
        self.m.append(hand)
    def send(self,a):
       self.m.hand(a)

on_ooo=Event()

for k, v in globals().items():
    if isinstance(v, Event):
        setattr(v, "name", k)

@on_ooo
def message(self,a):
    l=[]
    l.append(a)
on_ooo.send(4)#这里会调用 message()

具体参考

https://github.com/newlife/django-socketio/blob/master/django_socketio/events.py

2013-11-13修改了格式

django-socketio python django

纯洁的怪蜀黍 10 years, 5 months ago

拿余光瞟了下,第8行错了,你怎么在 renturn 后去 append 呢?

http://segmentfault.com/q/1010000000309309#a-1020000000309337 这个问题也是你提的吧?我在原来的答案里按你的代码修改了一下

Kamelot answered 10 years, 5 months ago

Your Answer