Last active
August 29, 2015 13:59
-
-
Save josephok/10524138 to your computer and use it in GitHub Desktop.
Python closure
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
def new_counter(): | |
i = 0 | |
def nested(): | |
nonlocal i | |
i += 1 | |
return i | |
return nested | |
c = new_counter() | |
print(c()) | |
print(c()) | |
print(c()) | |
print(c()) | |
print(c()) | |
print(c()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment