In Python, if I have a child function within a parent function, is the child function \"initialised\" (created) every time the parent function is called? Is there any overhe
The other answers are great and really answer the question well. I wanted to add that most inner functions can be avoided in python using for loops, generating functions, etc.
Consider the following Example:
def foo():
# I need to execute a function on two sets of arguments:
argSet1 = (1, 3, 5, 7)
argSet2 = (2, 4, 6, 8)
# A Function could be executed on each set of args
def bar(arg1, arg2, arg3, arg4):
return (arg1 + arg2 + arg3 + arg4)
total = 0
for argSet in [argSet1, argSet2]:
total += bar(*argSet)
print( total )
# Or a loop could be used on the argument sets
total = 0
for arg1, arg2, arg3, arg4 in [argSet1, argSet2]:
total += arg1 + arg2 + arg3 + arg4
print( total )
This example is a little goofy, but I hope you can see my point nonetheless. Inner functions are often not needed.