Skip to content

Instantly share code, notes, and snippets.

@daGrevis
Created March 5, 2014 09:59
Show Gist options
  • Save daGrevis/9364427 to your computer and use it in GitHub Desktop.
Save daGrevis/9364427 to your computer and use it in GitHub Desktop.
Counter using closure and non-local in Python 3
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