参数为数学函数的函数

function with parameter that is a mathematical func

我希望在 Python 中有这样的功能:

class EvaluationStrategy(object):
    def __init__(self, test_function):

    self.test_function = test_function

class TestFunction(object):
    def objective_function(self, design_variable1, design_variable2):
        print("external function called")
        intermediate_results = self.__internal_function_evaluation_()

        v1= intermediate_results(0)
        v2= intermediate_results(2)

        v=test_function

调用包含 EvaluationStrategy 的函数后,应根据 x 定义测试函数,例如 x^2 或类似的函数。但是如果 x 没有定义 Python 总是抛出一个错误所以我用 lambda 尝试了它但是如果没有定义 x 之前它也不起作用。 如果有人可以提前帮助me.Thanks。

不清楚你在问什么(见我的评论),但你需要的要素之一似乎是如何将函数作为参数传递并存储该函数以供以后执行。

以下是您可以这样做的方法:

class EvaluationStrategy:
    def __init__(self, test_function):
        self.test_function = test_function

    def evaluate(self, arg):
        return self.test_function(arg)

ev = EvaluationStrategy(lambda x: x**2)

for i in range(2,5):
    print(i, ev.evaluate(i))