[BangPypers] Python Gotcha Fibonacci
Rajiv Subramanian M
rajiv.m1991 at gmail.com
Sat Oct 4 13:27:50 CEST 2014
Hi Group,
I just went through this article
http://docs.python-guide.org/en/latest/writing/gotchas/
written a Fibonacci program like this
def fibonacci(next=[1], prev=[0]):
data = prev.pop()
prev.append(next.pop())
next.append(prev[0] + data)
return data
for i in range(10):
print fibonacci()
It actually works, it prints first 10 fibonacci numbers
My question is the following code doesn't work the same way as i expected
it to work.
def fibonacci(next=1, prev=0):
data = prev
prev = next
next = data + next
return data
for i in range(10):
print fibonacci()
Instead of printing Fibonacci, it just prints 10 zeros
means.. does python only remembers the list in default argument?
--
[image: --]
Rajiv Subramanian M
[image: http://]about.me/rajiv.m1991
<http://about.me/rajiv.m1991?promo=email_sig>
More information about the BangPypers
mailing list