0
Speak guys, I’m starting studies in Python and I came across a behavior a little strange for me. It’s this: I have a file test py. which contains some functions. In another file, exempl1.py imported this and other files as modules. In the file exempl1.py created a 3 functions(funct1, func2, funct3). In the funct1 i am passing as parameters: a list and reference of a function defined in test py.. Within funct1 I assigned the return of func2 to 3 variables, all with the same parameters for func2:
a = func2(x_best,h,0.1)
b = func2(x_best,h,0.1)
c = func2(x_best,h,0.1)
However, the variables are receiving the values as if func2 was executed 3 times, always receiving as parameters its own return from the previous execution. Another thing I realized is that if I declare only two variables, the values assigned to the variables is the equivalent of two executions of func2 receiving as parameters the return of the previous execution. My question is: is there an implicit recursion happening according to the amount of variables I declare??? I’m quite confused by these results.
Below I will leave the codes so that it is more clear what I am trying to say. I will omit some functions because they do not interfere with the problem. Thanks for your help.
test py.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import math
import json
from openpyxl import *
from random import *
from math import *
def beale ():
    return (1.5 - ponto[0]+ponto[0]*ponto[1])**2+(2.25-ponto[0]+ponto[0]*ponto[1]**2)**2+(2.625-ponto[0]+ponto[0]*ponto[1]**3)**2
def tgo(problema):
    pontos = points_generator(100)
    matriz = t_matriz_generator(pontos, problema)
    indices = list() 
    melhores = list()
    for i in range(len(matriz)):
        j = 0
        while (j < 4):
            if(matriz[i][j] < 0):
                break
            if (j >= 3):
                indices.append(i)
            j += 1
    for a in indices:
        melhores.append(pontos[a])
    return melhores, problema
exempl1.py
from random import *
from teste import *
import math
import time
def LineSearch(Xtrial, Xbest, d, h):
    while (problema(Xtrial) < problema(Xbest)):
        Xbest = Xtrial
        h *= 2
        Xtrial[i]=trial_search(Xbest,h,d)
    return Xbest, h
def trial_search(ponto, h, d):
    FinalPoint = ponto
    for i in range(len(FinalPoint)):
        FinalPoint[i] = (FinalPoint[i]+h)*d
        if (problema(FinalPoint) < problema(ponto)):
            continue
        else:
            FinalPoint[i] = ponto[i]
    return FinalPoint
def Unirandi(x0, problema):
    tol = 0.0001
    h = 1.0
    fails = 0
    x_best = x0
    a = trial_search(x_best,h,0.1)
    b = trial_search(x_best,h,0.1)
    c = trial_search(x_best,h,0.1)
p, problema = tgo(beale)
Unirandi([-0.05338583452963963, 4.311503781033389],problema)
The values for the variables are:
a -> [0.11094661416547036, 0.11531150378103339] 
b -> [0.11094661416547036, 0.11531150378103339] 
c -> [0.11094661416547036, 0.11531150378103339]
If I just declare
a = trial_search(x_best,h,0.1)
b = trial_search(x_best,h,0.1)
as a result I have:
a -> [0.10946614165470361, 0.15311503781033392] 
b -> [0.10946614165470361, 0.15311503781033392]
and finally if I declare only
a = trial_search(x_best,h,0.1)
as a result
a -> [0.09466141654703604, 0.531150378103339]