Is a, b = b, a + b a good python?

a, b = b, a + b 

I chose this coding style from Building Skills in Python. Based on PHP and Obj-C, this destination type with multiple variables is not available (at least not as I saw). But it seems more logical to me. After all, why should I assign 'a' to a holding variable before replacing it.

The question is, what is the pythonic coding style?

Here is an example Fibonacci program:

a, b = 1, 2
output = str(a)
while b<=100:
    output = output + " " + str(b)
    a, b = b, a + b

print output

And, what is the best way you found to help you write more python code?

+4
source share
2 answers

, . generator, ( ) :

def fib(k=100):
    """ Generate fibonacci numbers up to k. """
    a, b = 1, 2
    while a <= k:
        yield a
        a, b = b, a + b

print(' '.join(map(str, fib())))
# prints: '1 2 3 5 8 13 21 34 55 89'
+6

, pythonic. , , Python. , , .

, python. , . , collections .

+6

Source: https://habr.com/ru/post/1531252/


All Articles