Suppose I want to reuse some code, name it "function C" whose usage falls only under the scope of "function R". Nesting the function definition within R, serves to restrict its name in the local scope (the simplest possible closure, if it can be called such). Function C is not supposed to be returned, have any side effects or even refer to its outer scopes (except globals and imported modules)
def R(x):
def C(y):
return y
return C(x)
For a large number of R() calls, does this introduce any performance penalty over:
def C(x):
return x
def R(x)
return C(x)
Even if I import a module in the global scope and use it from within the closure, timings seem the same for both, if not faster for the first variation.
Excluding the accidental use of nonlocal variables, are there any pitfalls that make such usage of closures redundant or bug prone?
Note: I know that conformance to "Flat is better than nested" and readability of such constructs may be disputed but my question is oriented towards reasons other than readability and alike.