Skip to content

Instantly share code, notes, and snippets.

@alexboche
Created August 5, 2019 09:06
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 alexboche/459007a9f0687fb9d8a1b2051b73ec39 to your computer and use it in GitHub Desktop.
Save alexboche/459007a9f0687fb9d8a1b2051b73ec39 to your computer and use it in GitHub Desktop.
def make_averager():
count = 0
total = 0
def averager(new_value):
# nonlocal count, total # doesn't work
# global count, total
count = count + 1
total = total + new_value
print(total/count)
return total/count
return averager
averager = make_averager()
averager(1)
averager(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment