I know functions in Python are 1st class citizens, meaning that they are objects of the function
class similar to how 5
is an object of the int
class. This means that at some point in their life-time a constructor is called. For most functions I would expect this to happen when they are defined (as most functions are presumably defined only once) so that we only pay the construction price once no matter how many times we use it.
But what about nested functions? Those are redefined every time their parent is called. Does this mean we re-construct the object every time? If yes, isn't that grossly inefficient? Wouldn't a private method (assuming the parent function is a method) or another function be much more efficient? I am ignoring scoping arguments in favour of nesting for this discussion.
I run a simple experiment that seems to support my aforementioned argument as the inner function edition is slower:
import time
def f(x, y):
def defined_inside(x, y):
return x + y
return defined_inside(x, y)
def defined_outside(x, y):
return x + y
def g(x, y):
return defined_outside(x, y)
start = time.time()
for i in range(10000000):
_ = f(3, 4)
end = time.time()
print("Using inner function it took {} seconds".format(end - start))
start = time.time()
for i in range(10000000):
_ = g(3, 4)
end = time.time()
print("Using outer function it took {} seconds".format(end - start))
Results:
Using inner function it took 2.494696855545044 seconds
Using outer function it took 1.8862690925598145 seconds
Bonus question: In case the above is true, how does the situation relate to compiled languages, such as Scala? I grew into a huge fun of nesting and higher order functions, it would be terrible if the trick is as inefficient as it seems.