Python: force local scope using * persistent * closure

Suppose I want to reuse some code, name it "function C", the use of which falls only within the scope of the "function R". An embedding of a function definition inside R serves to restrict its name in the local area (the simplest closure, if one can call it that). Function C should not return, have any side effects or even refer to its external areas (except for globals and imported modules).

def R(x):
    def C(y):
        return y

    return C(x)

For a large number of R () calls, this leads to performance degradation:

def C(x):
    return x

def R(x)
    return C(x)

Even if I import the module into the global area and use it from the closure, the timings seem the same for both, if not faster for the first option.

, - , ?

. , " , ", , , .

+3

All Articles