Skip to content

Instantly share code, notes, and snippets.

@kissgyorgy
Last active August 29, 2015 14:18
Show Gist options
  • Save kissgyorgy/796bd7933bc7dbb2e122 to your computer and use it in GitHub Desktop.
Save kissgyorgy/796bd7933bc7dbb2e122 to your computer and use it in GitHub Desktop.
Python: Closure hack
# http://stackoverflow.com/a/23558809/720077
def fibonacci():
a, b = [1], [0]
def fibo():
a[0], b[0] = b[0], a[0] + b[0]
return a[0]
return fibo
fib = fibonacci()
for _ in range(10):
print fib(),
# 0 1 1 2 3 5 8 13 21 34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment