Last active
March 24, 2018 20:56
-
-
Save appeltel/8b7388f3b9efc92b851e9dba0ec5ad3d to your computer and use it in GitHub Desktop.
Sorry, We're Clopen.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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