def fib_m_through_n(m, n): """(number, number) -> list A function which returns a list containing the mth through the nth fibonacci numbers. SHOULD BE MODIFIED IN THE FUTURE so it will remember values already computed, so when it is called again it reuses that information """ fib_list = [0, 1, 1] a, b, c = 1, 1, 0 #why can I do three variable assignments here... count = 0 while count <= n: c = a + b; a = b; b = c #...but not here? fib_list.append(c) count += 1 print(fib_list[m : n]) fib_m_through_n(0, 17)
I have a few questions about this:
The first is general: are there any clear pythonic violations here, any obvious improvements to style?
The second is included in some of the comments. Why can I get away with assigning three variables in one line here:
a, b, c = 1, 1, 0
but not here:
c = a + b; a = b; b = c
If I try to do this:
c, a, b = a + b, b, c
Then I get an incorrect output.
After analyzing the mistake and trying to imagine how things look from the function's POV, it still seems to me like doing three variable assignments in a row should work, assuming that when the function runs it just reads this line from left to right.
Finally, as noted in the section just after the docstring, I want to turn this into a generator. I have read about generators and understand them a bit, but I don't know how to implement one here.