我正在使用Python API,希望我将它传递给函数。但是,由于各种原因,我想传递一个方法,因为我希望函数的行为不同,具体取决于它所属的实例。如果我传递一个方法,API将不会使用正确的'self'参数调用它,所以我想知道如何将方法转换为一个知道它属于'self'的函数。
我可以想到有几种方法可以做到这一点,包括使用lambda和闭包。我在下面列举了一些例子,但我想知道是否有一种标准机制可以达到同样的效果。
class A(object):
def hello(self, salutation):
print('%s, my name is %s' % (salutation, str(self)))
def bind_hello1(self):
return lambda x: self.hello(x)
def bind_hello2(self):
def hello2(*args):
self.hello(*args)
return hello2
>>> a1, a2 = A(), A()
>>> a1.hello('Greetings'); a2.hello('Greetings')
Greetings, my name is <__main__.A object at 0x71570>
Greetings, my name is <__main__.A object at 0x71590>
>>> f1, f2 = a1.bind_hello1(), a2.bind_hello1()
>>> f1('Salutations'); f2('Salutations')
Salutations, my name is <__main__.A object at 0x71570>
Salutations, my name is <__main__.A object at 0x71590>
>>> f1, f2 = a1.bind_hello2(), a2.bind_hello2()
>>> f1('Aloha'); f2('Aloha')
Aloha, my name is <__main__.A object at 0x71570>
Aloha, my name is <__main__.A object at 0x71590>
答案 0 :(得分:8)
将绑定到实例的方法传入工作吗?如果是这样,你不必做任何特别的事情。
In [2]: class C(object):
...: def method(self, a, b, c):
...: print a, b, c
...:
...:
In [3]: def api_function(a_func):
...: a_func("One Fish", "Two Fish", "Blue Fish")
...:
...:
In [4]: c = C()
In [5]: api_function(c.method)
One Fish Two Fish Blue Fish
答案 1 :(得分:0)
您可能想澄清一下您的问题。正如瑞恩指出的那样,
def callback(fn):
fn('Welcome')
callback(a1.hello)
callback(a2.hello)
将导致使用正确的hello
绑定,self
或a1
调用a2
。如果您没有遇到这种情况,那就会出现严重问题,因为这就是Python的工作原理。
根据你所写的内容,你似乎想要的是绑定论证 - 换句话说,currying。你可以找到各地的例子,但根据我的口味,Recipe 52549有最好的Pythonic外观。
class curry:
def __init__(self, fun, *args, **kwargs):
self.fun = fun
self.pending = args[:]
self.kwargs = kwargs.copy()
def __call__(self, *args, **kwargs):
if kwargs and self.kwargs:
kw = self.kwargs.copy()
kw.update(kwargs)
else:
kw = kwargs or self.kwargs
return self.fun(*(self.pending + args), **kw)
f1 = curry(a1.hello, 'Salutations')
f1() # == a1.hello('Salutations')