Skip to content

Instantly share code, notes, and snippets.

@josephok
Last active August 29, 2015 13:59
Show Gist options
  • Save josephok/10524138 to your computer and use it in GitHub Desktop.
Save josephok/10524138 to your computer and use it in GitHub Desktop.
Python closure
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