Re: indentation...

Rickard Westman (ricwe@ida.liu.se)
Mon, 18 Jul 1994 03:20:33 GMT

"Ron Forrester" <rjf@aurora.pcg.com> writes:

>I might as well as one of the questions I gleaned from the tutorial: In
>this code fragement:

>def fib(n):
> a, b = 0, 1
> while b <= n:
> print b,
> a, b = b, a+b

>What are the first ('a, b = 0, 1') and last ('a, b = b, a+b') lines
>doing?

Python sometimes allows you to omit parantheses when constructing tuples. In
my humble opinion, this is somewhat of a misfeature - the following program is
equivalent and looks cleaner:

def fib(n):
(a, b) = (0, 1)
while b <= n:
print b,
(a, b) = (b, a+b)

The semantics of tuple assignment is that the tuple on the right-hand side is
evaluated *in its entirety*, then its elements are assigned to the
corresponding elements on the left-hand side.

The first tuple assignment could thus be replaced by two scalar assignments
"a=0; b=1". The second assignment, however, would need at least three scalar
assignments to accomplish the same effect, e.g: "old_a=a; a=b; b=old_a+b"

--
Rickard Westman <ricwe@ida.liu.se>  |  "I think not."
Programming Environments Laboratory |
University of Linkvping, Sweden     |        - Descartes' last words?
------------------------------------+------------------------------------