如何在 Python 中将方法作为参数传递 [英] How do I pass a method as a parameter in Python

查看:74
本文介绍了如何在 Python 中将方法作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将方法作为参数传递给方法?

Is it possible to pass a method as a parameter to a method?

self.method2(self.method1)

def method1(self):
    return 'hello world'

def method2(self, methodToRun):
    result = methodToRun.call()
    return result

推荐答案

是的,只需使用您所写的方法名称即可.方法和函数是 Python 中的对象,就像其他任何东西一样,您可以像处理变量一样传递它们.实际上,您可以将方法(或函数)视为一个变量,其值是实际可调用的代码对象.

Yes it is, just use the name of the method, as you have written. Methods and functions are objects in Python, just like anything else, and you can pass them around the way you do variables. In fact, you can think about a method (or function) as a variable whose value is the actual callable code object.

由于您询问了方法,因此我在以下示例中使用了方法,但请注意,以下所有内容都同样适用于函数(除了没有 self 参数).

Since you asked about methods, I'm using methods in the following examples, but note that everything below applies identically to functions (except without the self parameter).

要调用传递的方法或函数,您只需使用它绑定的名称,就像使用方法(或函数)的常规名称一样:

To call a passed method or function, you just use the name it's bound to in the same way you would use the method's (or function's) regular name:

def method1(self):
    return 'hello world'

def method2(self, methodToRun):
    result = methodToRun()
    return result

obj.method2(obj.method1)

注意:我相信 __call__() 方法确实存在,即您可以从技术上讲methodToRun.__call__(),但您可能永远不应该明确这样做.__call__() 旨在实现,而不是从您自己的代码中调用.

Note: I believe a __call__() method does exist, i.e. you could technically do methodToRun.__call__(), but you probably should never do so explicitly. __call__() is meant to be implemented, not to be invoked from your own code.

如果您希望使用参数调用 method1,那么事情会变得有点复杂.method2 必须写一些关于如何将参数传递给 method1 的信息,它需要从某个地方获取这些参数的值.例如,如果 method1 应该接受一个参数:

If you wanted method1 to be called with arguments, then things get a little bit more complicated. method2 has to be written with a bit of information about how to pass arguments to method1, and it needs to get values for those arguments from somewhere. For instance, if method1 is supposed to take one argument:

def method1(self, spam):
    return 'hello ' + str(spam)

然后你可以编写 method2 用一个传入的参数调用它:

then you could write method2 to call it with one argument that gets passed in:

def method2(self, methodToRun, spam_value):
    return methodToRun(spam_value)

或者带有它自己计算的参数:

or with an argument that it computes itself:

def method2(self, methodToRun):
    spam_value = compute_some_value()
    return methodToRun(spam_value)

您可以将其扩展为传入值和计算值的其他组合,例如

You can expand this to other combinations of values passed in and values computed, like

def method1(self, spam, ham):
    return 'hello ' + str(spam) + ' and ' + str(ham)

def method2(self, methodToRun, ham_value):
    spam_value = compute_some_value()
    return methodToRun(spam_value, ham_value)

甚至使用关键字参数

def method2(self, methodToRun, ham_value):
    spam_value = compute_some_value()
    return methodToRun(spam_value, ham=ham_value)

如果你不知道,在写method2时,methodToRun要带什么参数,你也可以使用参数解包,通用的方式调用它:

If you don't know, when writing method2, what arguments methodToRun is going to take, you can also use argument unpacking to call it in a generic way:

def method1(self, spam, ham):
    return 'hello ' + str(spam) + ' and ' + str(ham)

def method2(self, methodToRun, positional_arguments, keyword_arguments):
    return methodToRun(*positional_arguments, **keyword_arguments)

obj.method2(obj.method1, ['spam'], {'ham': 'ham'})

在这种情况下,positional_arguments 需要是一个列表或元组或类似的,而 keyword_arguments 是一个 dict 或类似的.在 method2 中,您可以在调用 method1<之前修改 positional_argumentskeyword_arguments(例如添加或删除某些参数或更改值)/代码>.

In this case positional_arguments needs to be a list or tuple or similar, and keyword_arguments is a dict or similar. In method2 you can modify positional_arguments and keyword_arguments (e.g. to add or remove certain arguments or change the values) before you call method1.

这篇关于如何在 Python 中将方法作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆