Define a function inside loop

Viewed 409
def lin_min(alpha):#1
    return 1       #2
xk = init_path
for i in range(0, 50):
    sk = -dobj(xk)
    def lin_min(alpha):
        return obj(xk + alpha*sk)
    opt = sopt.golden(lin_min)
    xk = xk + opt * sk

I'm trying to define a function inside the loop. However, it does not work until I add the first two lines at the top. They have nothing to do with my function but the code works this time. Why is that?

0 Answers
Related