Skip to content

Instantly share code, notes, and snippets.

@sonkm3
Created July 28, 2012 02:58
Show Gist options
  • Save sonkm3/3191526 to your computer and use it in GitHub Desktop.
Save sonkm3/3191526 to your computer and use it in GitHub Desktop.
python generator by closure (count kept in property of function)
def get_counter():
print 'get_counter'
def counter():
counter.count = counter.count + 1
return counter.count
counter.count = 0
return counter
counter = get_counter()
for lp in range(5):
print counter()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment