Last active
August 29, 2015 14:18
-
-
Save kissgyorgy/796bd7933bc7dbb2e122 to your computer and use it in GitHub Desktop.
Python: Closure hack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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