函数与参数是一个数学函数

时间:2017-05-27 17:38:38

标签: python-3.x function mathematical-optimization mathematical-expressions

我想在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之前它也无法工作。 如果有人能帮助我的话。谢谢。

1 个答案:

答案 0 :(得分:0)

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

以下是如何做到这一点:

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))