0

I'd like to have a function like this in 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

After calling a function where EvaluationStrategy is contained, the testfunction e.g x^2 or something like that should be defined in dependence of x. But if x isn't defined Python always throws out an error so I tried it with lambda but it also doesn't work without defining x before. If anyone could please help me.Thanks in advance.

basti
  • 23
  • 4
  • Could you add a practical, concrete example of how you would call this, and what result you would expect? – trincot May 27 '17 at 18:05
  • I call a child function of EvaluationStrategy and have to insert a function... >>Evaluate(x**2) then Evaluate function assigns new values of x inside a loop and evaluates x^2 for each value Something like >>2 --> x^2 = 4 >>4 --> x^2 = 16 – basti May 28 '17 at 09:45
  • Sorry, I can't see the connection between all that. `Evaluate`? I don't see that function in your code sample. Where do you get the `x` value from, like the 2? Is it given in some method call? Can you provide an exact script you would like to be able to execute and provide the desired output for that? That will be helpful to determine which implementation you are looking for. – trincot May 28 '17 at 17:23

1 Answers1

0

It is not clear what you are asking (see my comments), but it seems one of the ingredients you need is how to pass a function as an argument and store that function for later execution.

Here is how you could do that:

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))
trincot
  • 317,000
  • 35
  • 244
  • 286
  • Thank you! This was the one I searched for, never tried to put the lambda thing into my input.---------------------- Would you help me with another issue, too? -------------------------- I need to understand why a while loop is creating arrays of arrays of arrays...and so on.--------------------- Everytime the next round is starting the old parameters change to new one's(as expected), BUT now appear as an array (next loop round: array[array[xy]] ) ------------------------------- Does this have something in common with the Error : object array may be self-referencing – basti May 30 '17 at 18:54
  • A `while` loop in itself does not need to have anything to do with lists, although it can be. Also, it does not force the behaviour you are describing if you do use it with lists. I would suggest that you post a new question about this, and include the code that is not behaving like you want it. If I am not around when you post it, there will be plenty of people around to help you. – trincot May 30 '17 at 19:19