I am looking at a debilitating performance problem in Python while testing code out in the IDLE GUI.
For a recursive function:
def f(input1,input2):
newinput1 = g(input1,input2);
return f(newinput1,input2)
If I call the function f(20,A+10) where A is a constant then does each recursive call of f() get input2 = "A+10" as a string that is reinterpreted, get an expression that needs to be recalculated, or get a number that is the result of A+10 ?
I found this in the help file, but need something more well defined to understand:
"Abstractions tend to create indirections and force the interpreter to work more. If the levels of indirection outweigh the amount of useful work done, your program will be slower. You should avoid excessive abstraction, especially under the form of tiny functions or methods (which are also often detrimental to readability)."
What exactly is going on in Python?
When you call a function as follows:
f(20, A+10)
Python evaluates 20
to 20
and A+10
to whatever that works out to. Let's say A
is currently 20
, so A+10
works out to 30
. The names input1
and input2
are then bound to the values 20
and 30
in the environment of the call to f
. Python will not need to reevaluate A+10
when the value is used, and it will not record anything about how the value 30
was obtained. In particular, if you call
f(20, A)
input2
will be bound to the current value of A
, but it will not retain any ties to A
. Reassigning input2
inside f
will not affect A
.