简体   繁体   English

Python:创建 class,它的构造函数接受用户给定的 function,它成为一个方法

[英]Python: Create class whose constructor accepts a user-given function which becomes a method

Is it possible in python (3) to have a class whose constructor takes as an argument a function which is then converted into a method on that instance?在 python (3) 中是否有可能有一个 class 的构造函数将 function 作为参数,然后将其转换为该实例上的方法?

I want to do something like:我想做类似的事情:

class Example:
    def __init__(self, val1, val2, user_passed_function):
        self.val1 = val1
        self.val2 = val2
        # this next line doesn't quite get what I'm going for...
        self.upf = user_passed_function

    def user_function_doer(self, *args, **kwargs):
        # THIS LINE IS KEY
        return self.upf(*args, **kwargs)

def upf1(instance, arg):
    return instance.val1 + arg

def upf2(instance, arg):
    return instance.val2 + arg

and then have the following:然后有以下内容:

ex1 = Example(1, 2, upf1)
ex1.user_function_doer(10)
## should return 1 + 10 = 11

ex2 = Example(1, 2, upf2)
ex2.user_function_doer(10)
## should return 2 + 10 = 12

My actual use case is a bit more complicated than this, but basically I want to be able to pass a function at instantiation time that, later on, will have the ability to access attributes on the instance.我的实际用例比这要复杂一些,但基本上我希望能够在实例化时传递一个 function,以便稍后能够访问实例上的属性。

Possible?可能的?

I believe if I change the line after the # THIS LINE IS KEY comment to read:我相信如果我将# THIS LINE IS KEY评论之后的行改为:

    def user_function_doer(self, *args, **kwargs):
        # THIS LINE IS KEY
        return self.upf(self, *args, **kwargs)

things might work out ok.事情可能会顺利进行。 But it feels odd to need to write the self argument explicitly there...for actual instance methods it would be passed automatically.但是需要在那里显式地编写self参数感觉很奇怪……对于实际的实例方法,它将自动传递。 Is there a way to get that to happen?有没有办法让它发生?

You have mentioned an instance argument in the definition of upf1 and upf2您在upf1upf2的定义中提到了一个instance参数
but do not pass self in return self.upf(*args, **kwargs)但不要通过self作为return self.upf(*args, **kwargs)

return self.upf(self,*args, **kwargs) solves that and seems to work as expected return self.upf(self,*args, **kwargs)解决了这个问题,并且似乎按预期工作

Also... you are missing the self in def user_function_doer(self,*args, **kwargs):另外...您在def user_function_doer(self,*args, **kwargs):中缺少self

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:将接受参数的函数传递给类方法? - Python: Pass a function that accepts arguments to a class method? 通过用户给定的正则表达式检查字符串 - Check string by user-given regex Python - 作为类属性的函数成为绑定方法 - Python - function as class attribute becomes a bound method 创建矩阵,其 position 值由 function 给出 - create matrix whose position value is given by a function 在Python中,给定实例从其继承特定方法的类 - In Python, class from which a given instance is inheriting a particular method 将参数传递给类,该类成为函数的关键字 - Pass an argument to a class which becomes a keyword for a function 创建一个将表达式作为python输入的函数 - Create a function that accepts an expression as an input in python 创建一个 python function 接受 dataframe 和列 - Create a python function that accepts dataframe and column(s) 如何调用 function 其名称由用户通过 Python 字典 w/o if-elif 语句给出 - How to call a function whose name is given by the user through Python dictionary w/o if-elif statements 通过将函数传递给Python中的类构造函数来轻松更改方法 - Slighly alter method by passing function to class constructor in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM