131

在 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 的方式将未绑定的方法绑定到实例并继续传递它而不调用它?

4

5 回答 5

194

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

bound_handler = handler.__get__(self, MyWidget)

这是 R. Hettinger 出色的描述符指南。


作为从基思的 评论中提取的一个独立的例子:

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
于 2009-06-18T21:54:29.527 回答
89

这可以使用types.MethodType干净地完成。例子:

import types

def f(self): 
    print(self)

class C: 
    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>>
于 2009-06-18T21:50:36.400 回答
10

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

self.method = (lambda self: lambda args: self.do(args))(self)
于 2011-09-05T19:31:03.830 回答
9

这将绑定selfhandler

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

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

于 2009-06-18T21:41:13.593 回答
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
于 2018-06-11T11:16:17.400 回答