Created
August 4, 2012 18:09
-
-
Save tayfun/3259093 to your computer and use it in GitHub Desktop.
Closures - Modifying variables closed over
This file contains 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/python3.2 | |
def create_counter(): | |
i = 0 | |
def increment(): | |
nonlocal i # this comes with python3; similar to the "global" keyword | |
i = i + 1 | |
print(i) | |
return increment | |
count = create_counter() | |
count() | |
count() | |
count() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment