Created
March 5, 2014 09:59
-
-
Save daGrevis/9364427 to your computer and use it in GitHub Desktop.
Counter using closure and non-local in Python 3
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 make_counter(n): | |
def inner(): | |
nonlocal n | |
n = n + 1 | |
return n | |
return inner | |
counter = make_counter(0) | |
print(counter()) | |
print(counter()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment