Last active
August 29, 2015 14:07
-
-
Save judy2k/01b8d19434090bce4df3 to your computer and use it in GitHub Desktop.
Writeable Closures
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
#!/usr/bin/env python | |
def outer(): | |
i = [0] | |
def inner(): | |
i[0] += 1 | |
print i[0] | |
return inner | |
f = outer() | |
f() | |
f() |
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
#!/usr/bin/env python3.4 | |
def outer(i): | |
def inner(): | |
nonlocal i | |
i += 1 | |
print(i) | |
return inner | |
f = outer(4) | |
f() | |
f() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment