Skip to content

Instantly share code, notes, and snippets.

@kurtbrose
Created November 19, 2012 20:44
Show Gist options
  • Save kurtbrose/4113750 to your computer and use it in GitHub Desktop.
Save kurtbrose/4113750 to your computer and use it in GitHub Desktop.
nonlocal
>>> def foo():
... a = 2
... def bar():
... print a
... return bar
...
>>> foo().func_closure
(<cell at 0x02515FF0: int object at 0x023AA6CC>,)
>>> foo().func_closure[0]
<cell at 0x02589ED0: int object at 0x023AA6CC>
>>> type(foo().func_closure[0])
<type 'cell'>
>>> b = foo()
>>> b()
2
>>> import ctypes
>>> ctypes.pythonapi
>>> ctypes.pythonapi.PyCell_Set(id(b.func_closure[0]), id(3))
0
>>> b()
3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment