__dict__.update() 是旧式的吗?

发布于 2024-11-28 04:32:39 字数 2999 浏览 0 评论 0原文

我对新式与旧式属性的差异感到困惑。在这个例子中:

  1. 这段代码是否使用旧式方法来修改属性?

    Event()中的self.__dict__.update(kw)

  2. 我是否使用 getattr、getattrgetattribute?

    对于 Dispatcher.dispatch_to_parent()

代码来自 http:// www.pygame.org/wiki/CommandDispatch?parent=CookBook

class Event(object):
    def __init__(self, name, kw={}):
        self.name = name
        self.__dict__.update(kw)
        self.dict = dict(kw)
        self.method_name = 'EVT_%s' % self.name

    def __repr__(self):
        return "<event>" % (self.name, self.dict)

class Dispatcher(object):
    def add(self, other):
        if isinstance(other, Dispatcher):
            other.parent = self
        else:
            raise ValueError("%s is not a Dispatcher" % other)

    def remove(self, other):
        if isinstance(other, Dispatcher):
            if other.parent is self:
                del other.parent
            else:
                raise ValueError("%s is not my child." % other)
        else:
            raise ValueError("%s is not a Dispatcher" % other)

    def dispatch(self, event):
        method = getattr(self, event.method_name, None)
        if method is not None: 
            return method(event)
        else:
            self.dispatch_to_parent(event)

    def dispatch_to_parent(self, event):
        parent = getattr(self, 'parent', None)
        if parent is not None:
            dispatch = getattr(parent, 'dispatch', None)
            if dispatch is not None: 
                return dispatch(event)

class Root(Dispatcher):
    def __init__(self):
        Dispatcher.__init__(self)
        self.focused_node = self

    def focus(self, node):
        if node is self.focused_node: return
        print 'focusing on', node
        self.focused_node.dispatch(Event('BLUR'))
        self.focused_node = node
        node.dispatch(Event('FOCUS'))

    def blur(self, node):
        self.focused_node = self
        node.dispatch(Event('BLUR'))

    def post(self, event_name, kw):
        event = Event(event_name, kw)
        return self.focused_node.dispatch(event)

if __name__ == "__main__":
    class Button(Dispatcher):
        def EVT_KeyDown(self, event):
            return self
    #create the root node, which is the receiver of all events.        
    r = Root()
    #create a button node
    b = Button()
    #add the button node to the application
    r.add(b)
    #give the button node focus, it will now receive events
    r.focus(b)
    #post a KeyDown event, which the button will receive
    print r.post('KeyDown')
    #post a Quit event, which the button will not receive. 
    #this event will be passed onto the button parent
    print r.post('Quit')
    #the return value of the post method is the node which handled the event

I'm getting confused reading about the differences in newstyle vs oldstyle attributes. In this example:

  1. Is this code using old-style method to modify attributes?

    In Event() at self.__dict__.update(kw)

  2. Do I use getattr, getattr or getattribute ?

    for Dispatcher.dispatch_to_parent()

The code is from http://www.pygame.org/wiki/CommandDispatch?parent=CookBook

class Event(object):
    def __init__(self, name, kw={}):
        self.name = name
        self.__dict__.update(kw)
        self.dict = dict(kw)
        self.method_name = 'EVT_%s' % self.name

    def __repr__(self):
        return "<event>" % (self.name, self.dict)

class Dispatcher(object):
    def add(self, other):
        if isinstance(other, Dispatcher):
            other.parent = self
        else:
            raise ValueError("%s is not a Dispatcher" % other)

    def remove(self, other):
        if isinstance(other, Dispatcher):
            if other.parent is self:
                del other.parent
            else:
                raise ValueError("%s is not my child." % other)
        else:
            raise ValueError("%s is not a Dispatcher" % other)

    def dispatch(self, event):
        method = getattr(self, event.method_name, None)
        if method is not None: 
            return method(event)
        else:
            self.dispatch_to_parent(event)

    def dispatch_to_parent(self, event):
        parent = getattr(self, 'parent', None)
        if parent is not None:
            dispatch = getattr(parent, 'dispatch', None)
            if dispatch is not None: 
                return dispatch(event)

class Root(Dispatcher):
    def __init__(self):
        Dispatcher.__init__(self)
        self.focused_node = self

    def focus(self, node):
        if node is self.focused_node: return
        print 'focusing on', node
        self.focused_node.dispatch(Event('BLUR'))
        self.focused_node = node
        node.dispatch(Event('FOCUS'))

    def blur(self, node):
        self.focused_node = self
        node.dispatch(Event('BLUR'))

    def post(self, event_name, kw):
        event = Event(event_name, kw)
        return self.focused_node.dispatch(event)

if __name__ == "__main__":
    class Button(Dispatcher):
        def EVT_KeyDown(self, event):
            return self
    #create the root node, which is the receiver of all events.        
    r = Root()
    #create a button node
    b = Button()
    #add the button node to the application
    r.add(b)
    #give the button node focus, it will now receive events
    r.focus(b)
    #post a KeyDown event, which the button will receive
    print r.post('KeyDown')
    #post a Quit event, which the button will not receive. 
    #this event will be passed onto the button parent
    print r.post('Quit')
    #the return value of the post method is the node which handled the event

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情域 2024-12-05 04:32:39

您不应该直接更新实例字典,您将绕过自定义设置器。循环按键并执行 setattr。

You shouldn't update the instance dictionary directly, you will bypass custom setters. Loop over the keys and do setattr.

我的鱼塘能养鲲 2024-12-05 04:32:39

def post(self, event_name, kw): 行中缺少 *

由于使您的函数能够接收字典作为参数,您需要在变量名称之前添加 **:

def post(self, event_name, **kw):
    event = Event(event_name, kw)
    return self.focused_node.dispatch(event)

There's a missing * in def post(self, event_name, kw): line

In due to make you function able to receive a dict as a parameter, you need to add ** before variables name:

def post(self, event_name, **kw):
    event = Event(event_name, kw)
    return self.focused_node.dispatch(event)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文