Python:绑定一个未绑定的方法?

时间:2009-06-18 21:33:19

标签: python class methods bind

在Python中,有没有办法绑定未绑定的方法而不调用它?

我正在编写一个wxPython程序,对于某个类,我认为将所有按钮的数据组合在一起作为类级别的元组列表是很好的,如下所示:

class MyWidget(wx.Window):
    buttons = [("OK", OnOK),
               ("Cancel", OnCancel)]

    # ...

    def Setup(self):
        for text, handler in MyWidget.buttons:

            # This following line is the problem line.
            b = wx.Button(parent, label=text).Bind(wx.EVT_BUTTON, handler)

问题是,由于handler的所有值都是未绑定的方法,我的程序会以惊人的火焰爆炸而且我会哭泣。

我在网上寻找一个解决方案似乎应该是一个相对简单,可解决的问题。不幸的是我找不到任何东西。现在,我正在使用functools.partial来解决这个问题,但是有没有人知道是否有一种干净,健康,Pythonic的方式将未绑定的方法绑定到一个实例并继续传递它而不调用它? / p>

5 个答案:

答案 0 :(得分:154)

所有函数也是描述符,因此您可以通过调用__get__方法绑定它们:

bound_handler = handler.__get__(self, MyWidget)

这是R. Hettinger对描述符的优秀guide


作为从Keith's comment提取的自包含示例:

def bind(instance, func, as_name=None):
    """
    Bind the function *func* to *instance*, with either provided name *as_name*
    or the existing name of *func*. The provided *func* should accept the 
    instance as the first argument, i.e. "self".
    """
    if as_name is None:
        as_name = func.__name__
    bound_method = func.__get__(instance, instance.__class__)
    setattr(instance, as_name, bound_method)
    return bound_method

class Thing:
    def __init__(self, val):
        self.val = val

something = Thing(21)

def double(self):
    return 2 * self.val

bind(something, double)
something.double()  # returns 42

答案 1 :(得分:76)

这可以通过types.MethodType干净利落地完成。例如:

import types

def f(self): print self

class C(object): pass

meth = types.MethodType(f, C(), C) # Bind f to an instance of C
print meth # prints <bound method C.f of <__main__.C object at 0x01255E90>>

答案 2 :(得分:9)

在其中创建一个带有self的闭包在技术上不会绑定该函数,但它是解决相同(或非常相似)底层问题的另一种方法。这是一个简单的例子:

self.method = (lambda self: lambda args: self.do(args))(self)

答案 3 :(得分:6)

这会将self绑定到handler

bound_handler = lambda *args, **kwargs: handler(self, *args, **kwargs)

这可以通过将self作为函数的第一个参数传递。 object.function()只是function(object)的语法糖。

答案 4 :(得分:1)

晚会,但我带着类似的问题来到这里:我有一个类方法和一个实例,并希望将该实例应用于该方法。

冒着过度简化OP问题的风险,我最终做了一些不那么神秘的事情,对于到达这里的人来说可能有用(警告:我在Python 3中工作 - YMMV)。

考虑这个简单的类:

class Foo(object):

    def __init__(self, value):
        self._value = value

    def value(self):
        return self._value

    def set_value(self, value):
        self._value = value

以下是您可以用它做的事情:

>>> meth = Foo.set_value   # the method
>>> a = Foo(12)            # a is an instance with value 12
>>> meth(a, 33)            # apply instance and method
>>> a.value()              # voila - the method was called
33
相关问题