Skip to content

Instantly share code, notes, and snippets.

@tmeissner tmeissner/py3_closures.py
Last active Aug 29, 2015

Embed
What would you like to do?
Some experiments with closures in Python 3
def closure():
container = 0
def inc():
nonlocal container
container += 1
def get():
return container
def dec():
nonlocal container
container -= 1
def set(item):
nonlocal container
container = item
return inc, dec, set, get
if __name__ == "__main__":
inc, dec, set, get = closure()
set(1)
for i in range(10):
inc()
for i in range(5):
dec()
print(get())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.