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

问题描述 投票:102回答:5

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

python class methods bind
5个回答
156
投票

所有函数也都是描述符,因此您可以通过调用它们的__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

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>>

9
投票

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

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

6
投票

这将self绑定到handler

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

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


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
© www.soinside.com 2019 - 2024. All rights reserved.