简体   繁体   English

Python,如何将函数作为参数传递给类方法

[英]Python, How to pass function to a class method as argument

I am trying write a signal processing package, and I want to let user creates a custom function without needing to access the class file : 我正在尝试编写一个信号处理包,我想让用户创建自定义函数而无需访问类文件:

class myclass():
    def __init__(self):
        self.value = 6
    def custom(self, func, **kwargs):
        func(**kwargs)
        return self

c = myclass()
def add(**kwargs):
    self.value +=  kwargs['val']

kwargs = {'val': 4}
c.custom(add, **kwargs )
print (c.value)

I got name 'self' is not defined. 我没有定义“自我”这个名字。 Of course because func is not a method to the class. 当然,因为func不是该类的方法。 But I am not sure how to fix it. 但是我不确定如何解决它。 Please advice. 请指教。

Thanks 谢谢

You can explicitly pass the self argument to add : 您可以显式传递self参数来add

class myclass():
    def __init__(self):
        self.value = 6
    def custom(self, func, **kwargs):
        func(self, **kwargs)
        return self

c = myclass()
def add(self, **kwargs):
    self.value +=  kwargs['val']

kwargs = {'val': 4}
c.custom(add, **kwargs )
print (c.value)

Output: 输出:

10

You need to pass the class instance into the method too, do this : 您也需要将类实例传递到方法中,执行以下操作:

class myclass():
    def __init__(self):
        self.value = 6
    def custom(self, func, **kwargs):
        func(self, **kwargs) ## added self here
        return self

c = myclass()
def add(self, **kwargs):  ## added self here
    self.value +=  kwargs['val']

kwargs = {'val': 4}
c.custom(add, **kwargs )
print (c.value)

output : 10 输出: 10

I would do one of the following: 我将执行以下操作之一:

1) Make the custom method a method of the class. 1)使自定义方法成为类的方法。

class myclass():
    def __init__(self):
        self.value = 6
    def custom(self, func, **kwargs):
        func(**kwargs)
        return self
    def add(self, **kwargs):
        self.value +=  kwargs['val']

kwargs = {'val': 4}
c = myclass()
c.custom( c.add, **kwargs )
print (c.value)    # Result == 10

2) Do not give the custom value itself access to class variables. 2)不要让自定义值本身访问类变量。

class myclass():
    def __init__(self):
        self.value = 6
    def custom(self, func, **kwargs):
        self.value += func(**kwargs)
        return self


def add(**kwargs):
    return kwargs['val']

kwargs = {'val': 4}
c = myclass()
c.custom(add, **kwargs )
print (c.value)    # Result == 10

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

相关问题 如何在Python类函数中隐式传递参数 - How to pass argument implicitly in Python Class function Python:在 function 中将方法作为参数传递 - Python: pass method as argument in function 如何将类方法作为参数传递给该类外部的函数? - How to pass a class method as an argument to a function external to that class? 如何将 function 作为 class 成员的参数传递给 Class 将用于重复评估该成员的方法? - How to pass a function as an argument for a class member that the Class will use in a method to repeatedly evaluate that member? Python - 将类作为参数传递给该类中方法的装饰器? - Python - Pass class as argument to decorator of method within that class? 如何在python中将函数传递给类的方法? - How can I pass a function to a class's method in python? 将对象方法作为函数参数传递给python中的方法链接 - pass object method as function argument for method chaining in python Python:将接受参数的函数传递给类方法? - Python: Pass a function that accepts arguments to a class method? Python,当参数为None时,如何不将关键字参数传递给方法? - Python, How not to pass a keyword argument to a method when the argument is None? 如何将整数系列作为参数传递给python函数? - How to pass an integer series as argument to a python function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM