请看下面的例子:有没有办法将参数绑定到python中的函数变量?
class foo:
def __init__(self):
print ("Constructor")
def testMethod(self,val):
print ("Hello " + val)
def test(self):
ptr = self.testMethod("Joe") <---- Anyway instead of calling self.testMethod with parameter "Joe" I could simple bind the parameter Joe to a variable ?
ptr()
k = foo()
k.test()
在把定义test
是有可能,我可以创建调用时调用该方法self.testMethod
与参数"Joe"
变量?
为什么现在这是一个downvote?无论如何,我可以改善这一点?没有任何线索的downvoting不是真的有生产力 –
你的意思是:'def test(self,name):ptr = self.testMethod(name)'?然后你可以调用k.test(“Joe”) – marcanuy
你的意思就像'from functools import partial',然后'self.callable = partial(self.testMethod,'Joe')'? –