将对象与对象方法一起传递给函数 [英] Pass object along with object method to function

查看:95
本文介绍了将对象与对象方法一起传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在Python中,如果您想将两个参数传递给一个函数,一个参数是一个对象,而另一个参数指定必须在该对象上调用的实例方法,则用户可以轻松地传递该对象本身,连同方法的名称(作为字符串)一起使用,然后在对象上使用getattr函数,并在字符串上使用对象上的方法.

现在,我想知道是否存在一种方法(如在C ++中,对于那些知道的人),以及在其中传递实际对象的方法(或更确切地说是对方法的引用,但没有方法名称)作为字符串).一个例子:

def func(obj, method):
    obj.method();

我尝试将其传递如下:

func(obj, obj.method)

或为

func(obj, classname.method)

但是都没有用(我知道的第二个有点远,但是我还是尝试了)

我知道您也可以定义一个仅接受该方法的函数,然后将其称为

func2(obj.method)

但是我特别询问的是需要引用对象本身的实例,以及要在对象上调用的所需类实例(非静态)方法的 reference ./p>

对于那些感兴趣的人,我发现了一种很优雅的方式,它受到以下公认答案的启发".我只是将func定义为

def func(obj, method):
     #more code here
     method(obj, parameter);     #call method on object obj

并命名为

func(obj_ref, obj_class.method);

其中obj_class是obj_ref是其实例的实际类.

解决方案

方法仅仅是将第一个参数绑定到实例的函数.因此,您可以做类似的事情.

# normal_call 
result = "abc".startswith("a")

# creating a bound method 
method = "abc".startswith
result = method("a") 

# using the raw function 
function = str.startswith
string = "abc"
result = function(string, "a") 

I know that in Python that if, say you want to pass two parameters to a function, one an object, and another that specifies the instance method that must be called on the object, the user can easily pass the object itself, along with the name of the method (as a string) then use the getattr function on the object and the string to call the method on the object.

Now, I want to know if there is a way (as in C++, for those who know) where you pass the object, as well as the actual method (or rather a reference to the method, but not the method name as a string). An example:

def func(obj, method):
    obj.method();

I have tried passing it as follows:

func(obj, obj.method)

or as

func(obj, classname.method)

but neither works (the second one I know was a bit of a long shot, but I tried it anyway)

I know that you can also just define a function that just accepts the method, then call it as

func2(obj.method)

but I am specifically asking about instances where you want a reference to the object itself, as well as a reference to a desired class instance (not static) method to be called on the object.

EDIT:

For those that are interested, I found quite an elegant way 'inspired' by the accepted answer below. I simply defined func as

def func(obj, method):
     #more code here
     method(obj, parameter);     #call method on object obj

and called it as

func(obj_ref, obj_class.method);

where obj_class is the actual class that obj_ref is an instance of.

解决方案

A method is just a function with the first parameter bound to an instance. As such you can do things like.

# normal_call 
result = "abc".startswith("a")

# creating a bound method 
method = "abc".startswith
result = method("a") 

# using the raw function 
function = str.startswith
string = "abc"
result = function(string, "a") 

这篇关于将对象与对象方法一起传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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