This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author meador.inge
Recipients Yury.Selivanov, eric.snow, meador.inge, ncoghlan
Date 2011-09-29.20:40:30
SpamBayes Score 0.0011276668
Marked as misclassified No
Message-id <1317328831.51.0.156802651703.issue13062@psf.upfronthosting.co.za>
In-reply-to
Content
I'll take a shot and writing a patch for this one.  Nick, are the 
elements in 'co_freevars' and '__closures__' always expected to match
up?  In other words, is the 'closure' function below always expected to 
work (simplified; no error checking):

>>> def make_adder(x):
...     def add(y):
...         return x + y
...     return add
... 
>>> def curry(func, arg1):
...     return lambda arg2: func(arg1, arg2)
... 
>>> def less_than(a, b):
...     return a < b
... 
>>> greater_than_five = curry(less_than, 5)
>>> def closure(func):
...     vars = [var for var in func.__code__.co_freevars]
...     values = [cell.cell_contents for cell in func.__closure__]
...     return dict(zip(vars, values))
...
>>> inc = make_adder(1)
>>> print(closure(inc))
{'x': 1}
>>> print(closure(greater_than_five))
{'arg1': 5, 'func': <function less_than at 0xb74c6924>}

?
History
Date User Action Args
2011-09-29 20:40:31meador.ingesetrecipients: + meador.inge, ncoghlan, Yury.Selivanov, eric.snow
2011-09-29 20:40:31meador.ingesetmessageid: <1317328831.51.0.156802651703.issue13062@psf.upfronthosting.co.za>
2011-09-29 20:40:30meador.ingelinkissue13062 messages
2011-09-29 20:40:30meador.ingecreate