Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@appeltel
Last active March 24, 2018 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save appeltel/8b7388f3b9efc92b851e9dba0ec5ad3d to your computer and use it in GitHub Desktop.
Save appeltel/8b7388f3b9efc92b851e9dba0ec5ad3d to your computer and use it in GitHub Desktop.
Sorry, We're Clopen.
Python 3.6.4 (default, Mar 23 2018, 08:00:10)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def opencell_gen(i):
... a = i
... def f(b):
... b = a
... a = yield f.__closure__[0]
... while True:
... a = yield None
...
>>> def replace_cells(f, cells):
... return type(f)(
... f.__code__,
... f.__globals__,
... f.__name__,
... f.__defaults__,
... cells
... )
...
>>> def make_adder(x):
... """Make a simple closure"""
... def add(y):
... return x + y
... return add
...
>>> adder = make_adder(1)
>>> adder(2)
3
>>> controller = opencell_gen(5)
>>> opencell = next(controller)
>>> adder = replace_cells(adder, (opencell,))
>>> adder(2)
7
>>> controller.send(10)
>>> adder(5)
15
>>> controller.send(20)
>>> adder(5)
25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment