Python 3 update - February 2019
Intentionally writing this out of the blue:
def f(x): def g(y): return x + y return g f2 = f(2) def closedVars(anF): keys = f2.__code__.co_freevars values = [cell.cell_contents for cell in f2.__closure__]
What is less clear to me, and this applies to the answer marked as correct, as well as to the question of whether the order between the tuple (nonlocal closure) of variable names ( __code__.co_freevars ) and the order of variable values ( f2.__closure__ ) are guaranteed to match (what the zip operation depends on). In the simple example used to pose the question, we are dealing with only one variable x , so the above will be enough for this particular case.
Would it be nice if someone could confirm the general case, and not just assume that this is so?
arcseldon
source share