+ 1

Iterators? Python

When i was looking for fibonaccis sequence code to compare to mine i found one answer i actually dont understand: def F(): a,b = 0,1 while True: yield a a, b = b, a + b especially the last line is totally strange. Read that if language supports iterators you can make such code. What does it mean?

29th Oct 2018, 10:51 AM
Michał Dőring
Michał Dőring - avatar
2 Respuestas
+ 2
In Python you can write a, b = b, a which means that a and b switch their values. If you wrote a = b and then b = a, both a and b would end up with the same value. In other languages you would need an extra line that keeps the in-between value, or a more complicated expression. So when you have a number of names on the left side separated by commas and on the right side the same number of values (or expressions), they will be assigned simultaneously.
29th Oct 2018, 11:22 AM
HonFu
HonFu - avatar