Skip to content

Instantly share code, notes, and snippets.

@tayfun
Created August 4, 2012 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tayfun/3259093 to your computer and use it in GitHub Desktop.
Save tayfun/3259093 to your computer and use it in GitHub Desktop.
Closures - Modifying variables closed over
#!/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