My problem is advanced and out of the scope of the question. So I’ll keep it simple. Imagine the following:
This is a set of helper functions, they only serve to make other functions more readable.
def functionA(*args, **kwargs):
...
def functionB(*args, **kwargs):
...
def functionC(*args, **kwargs):
...
But then I have my main functions:
def mainA(*args, **kwargs):
...
a = functionA(*args, **kwargs)
...
def mainB(*args, **kwargs):
...
b = functionB(*args, **kwargs)
...
def mainC(*args, **kwargs):
...
c = functionC(*args, **kwargs)
...
Thus functionA is only used by mainA ever! Etc. For functionB, C and mainB and C.
Would it be mode optimized to do this then?
def mainA(*args, **kwargs):
def functionA(*args, **kwargs):
...
...
a = functionA(*args, **kwargs)
...
def mainB(*args, **kwargs):
def functionB(*args, **kwargs):
...
...
b = functionB(*args, **kwargs)
...
def mainC(*args, **kwargs):
def functionC(*args, **kwargs):
...
...
c = functionC(*args, **kwargs)
...
My experience from C says, when the file is analysised, the functions would be created and called once. But if they are nested in the functions, they will be created on every call. Thus reduce performance.
Nesting them is purely for readability and keeping everything together. But am I impacting performace here?
Edit: similarly this can be used in classes, does that also possibly affect performance?