[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Beginner's assignment question

Schizoid Man

3/1/2008 2:40:00 PM

As in variable assignment, not homework assignment! :)

I understand the first line but not the second of the following code:

a, b = 0, 1
a, b = b, a + b

In the first line a is assigned 0 and b is assigned 1 simultaneously.

However what is the sequence of operation in the second statement? I;m
confused due to the inter-dependence of the variables.
8 Answers

Colin J. Williams

3/1/2008 3:47:00 PM

0

Schizoid Man wrote:
> As in variable assignment, not homework assignment! :)
>
> I understand the first line but not the second of the following code:
>
> a, b = 0, 1
> a, b = b, a + b
>
> In the first line a is assigned 0 and b is assigned 1 simultaneously.
>
> However what is the sequence of operation in the second statement? I;m
> confused due to the inter-dependence of the variables.
In the second line a now points to the
value 1 and b points to 1 (the sum of
zero and one) in a unitary operation
(i.e. in turn, but effectively
simultaneously).

You might disassemble the code to see
the sequence.

Colin W.

Lorenzo Gatti

3/1/2008 4:08:00 PM

0

On Mar 1, 3:39 pm, Schizoid Man <sc...@lon.don> wrote:
> As in variable assignment, not homework assignment! :)
>
> I understand the first line but not the second of the following code:
>
> a, b = 0, 1
> a, b = b, a + b
>
> In the first line a is assigned 0 and b is assigned 1 simultaneously.
>
> However what is the sequence of operation in the second statement? I;m
> confused due to the inter-dependence of the variables.

The expressions of the right of the assignment operator are evaluated
before assigning any new values, to the destinations on the left side
of the assignment operator.
So substitutig the old values of a and b the second assignment means

a, b = 0, 0 + 1

Simplifying the Python Reference Manual ("6.3 Assignment Statements")
a little :

assignment_stmt ::= target_list "="+ expression_list

An assignment statement evaluates the expression list (remember that
this can be a single expression or a comma-separated list, the latter
yielding a tuple) and assigns the single resulting object to each of
the target lists, from left to right.

[...]

WARNING: Although the definition of assignment implies that overlaps
between the left-hand side and the right-hand side are `safe' (for
example "a, b = b, a" swaps two variables), overlaps within the
collection of assigned-to variables are not safe! For instance, the
following program prints "[0, 2]":

x = [0, 1]
i = 0
i, x[i] = 1, 2
print x

Lorenzo Gatti




Aaron Brady

3/1/2008 6:16:00 PM

0

On Mar 1, 10:07 am, Lorenzo Gatti <ga...@dsdata.it> wrote:
> On Mar 1, 3:39 pm, Schizoid Man <sc...@lon.don> wrote:
>
> > As in variable assignment, not homework assignment! :)
>
> > I understand the first line but not the second of the following code:
>
> > a, b = 0, 1
> > a, b = b, a + b
>
> > In the first line a is assigned 0 and b is assigned 1 simultaneously.
>
> > However what is the sequence of operation in the second statement? I;m
> > confused due to the inter-dependence of the variables.
>
> The expressions of the right of the assignment operator are evaluated
> before assigning any new values, to the destinations on the left side
> of the assignment operator.
> So substitutig the old values of a and b the second assignment means
>
> a, b = 0, 0 + 1
>
> Simplifying the Python Reference Manual ("6.3 Assignment Statements")
> a little :
>
> assignment_stmt ::= target_list "="+ expression_list
>
> An assignment statement evaluates the expression list (remember that
> this can be a single expression or a comma-separated list, the latter
> yielding a tuple) and assigns the single resulting object to each of
> the target lists, from left to right.
>
> [...]
>
> WARNING: Although the definition of assignment implies that overlaps
> between the left-hand side and the right-hand side are `safe' (for
> example "a, b = b, a" swaps two variables), overlaps within the
> collection of assigned-to variables are not safe! For instance, the
> following program prints "[0, 2]":
>
> x = [0, 1]
> i = 0
> i, x[i] = 1, 2
> print x
>
> Lorenzo Gatti

If you understand

a, b, c= 1, 2, 3

Then

a, b, c= 1, 2, 3+1

is the next step.

About terminology, you might get some weird glances from the vets. if
you say 'a assigned to 0', instead of '0 assigned to a'. Of course,
it may be more opinion than some vet. natural language speakers
realize, that is whether it's only convention or not, if one says 'I
went to the store' instead of 'the store went to me'-- same picture,
except (cf. relativity) how much force the store applied to travel--
which doesn't show up in every story problem anyway), hence Williams's
term '0 is assigned to a', and don't forget, they're on side effects
here, hence Gatti's warning. Put another way, the thing on the right
'evaluates' to something.

There may be some 'agent-patient' confusion here.... but 'a' isn't
really 'assigned' in any deep sense, though it is 'declared' -in- that
statement too. 'The interpreter obtains space for a' (the space word
is deep!, as in 'time-space trade-off') and 'The interpreter writes 0
in a's space' are both pretty literal. You can say, 'a is assigned
the value 0', just not 'a is assigned -to- it.'

Schizoid Man

3/2/2008 10:26:00 AM

0

Lorenzo Gatti wrote:
> On Mar 1, 3:39 pm, Schizoid Man <sc...@lon.don> wrote:
>> As in variable assignment, not homework assignment! :)
>>
>> I understand the first line but not the second of the following code:
>>
>> a, b = 0, 1
>> a, b = b, a + b
>>
>> In the first line a is assigned 0 and b is assigned 1 simultaneously.
>>
>> However what is the sequence of operation in the second statement? I;m
>> confused due to the inter-dependence of the variables.
>
> The expressions of the right of the assignment operator are evaluated
> before assigning any new values, to the destinations on the left side
> of the assignment operator.
> So substitutig the old values of a and b the second assignment means
>
> a, b = 0, 0 + 1
>
> Simplifying the Python Reference Manual ("6.3 Assignment Statements")
> a little :
>
> assignment_stmt ::= target_list "="+ expression_list
>
> An assignment statement evaluates the expression list (remember that
> this can be a single expression or a comma-separated list, the latter
> yielding a tuple) and assigns the single resulting object to each of
> the target lists, from left to right.
>
> [...]
>
> WARNING: Although the definition of assignment implies that overlaps
> between the left-hand side and the right-hand side are `safe' (for
> example "a, b = b, a" swaps two variables), overlaps within the
> collection of assigned-to variables are not safe! For instance, the
> following program prints "[0, 2]":
>
> x = [0, 1]
> i = 0
> i, x[i] = 1, 2
> print x
>
> Lorenzo Gatti

Thank you for the explanation. I guess my question can be simplified as:

First step: a, b = 0, 1
No problem here as a and b are assigned values.

Second step: a, b = b, a + b

Now my question is does b become a + b after a becomes 1 or while a
stays at 0?

As the assignment occurs simultaneously I suppose the answer is while a
stays at 0.

Thank you.

Gabriel Genellina

3/2/2008 10:50:00 AM

0

En Sun, 02 Mar 2008 08:25:49 -0200, Schizoid Man <schiz@lon.don> escribi�:

> Lorenzo Gatti wrote:
>> On Mar 1, 3:39 pm, Schizoid Man <sc...@lon.don> wrote:
>>> As in variable assignment, not homework assignment! :)
>>>
>>> I understand the first line but not the second of the following code:
>>>
>>> a, b = 0, 1
>>> a, b = b, a + b
>>>
>>> In the first line a is assigned 0 and b is assigned 1 simultaneously.
>>>
>>> However what is the sequence of operation in the second statement? I;m
>>> confused due to the inter-dependence of the variables.
>>
>> The expressions of the right of the assignment operator are evaluated
>> before assigning any new values, to the destinations on the left side
>> of the assignment operator.
>> So substitutig the old values of a and b the second assignment means
>>
>> a, b = 0, 0 + 1
>>
>> Simplifying the Python Reference Manual ("6.3 Assignment Statements")
>> a little :
>>
>> assignment_stmt ::= target_list "="+ expression_list
>>
>> An assignment statement evaluates the expression list (remember that
>> this can be a single expression or a comma-separated list, the latter
>> yielding a tuple) and assigns the single resulting object to each of
>> the target lists, from left to right.
>>
>> [...]
>>
>> WARNING: Although the definition of assignment implies that overlaps
>> between the left-hand side and the right-hand side are `safe' (for
>> example "a, b = b, a" swaps two variables), overlaps within the
>> collection of assigned-to variables are not safe! For instance, the
>> following program prints "[0, 2]":
>>
>> x = [0, 1]
>> i = 0
>> i, x[i] = 1, 2
>> print x
>>
>> Lorenzo Gatti
>
> Thank you for the explanation. I guess my question can be simplified as:
>
> First step: a, b = 0, 1
> No problem here as a and b are assigned values.
>
> Second step: a, b = b, a + b
>
> Now my question is does b become a + b after a becomes 1 or while a
> stays at 0?
>
> As the assignment occurs simultaneously I suppose the answer is while a
> stays at 0.

Read the previous response carefully and you'll answer your question. The
right hand side is EVALUATED in full before values are assignated to the
left hand side. Evaluating b, a+b results in 1, 1. The, those values are
assigned to a, b.

--
Gabriel Genellina

Gabriel Genellina

3/2/2008 10:50:00 AM

0

En Sun, 02 Mar 2008 08:25:49 -0200, Schizoid Man <schiz@lon.don> escribi�:

> Lorenzo Gatti wrote:
>> On Mar 1, 3:39 pm, Schizoid Man <sc...@lon.don> wrote:
>>> As in variable assignment, not homework assignment! :)
>>>
>>> I understand the first line but not the second of the following code:
>>>
>>> a, b = 0, 1
>>> a, b = b, a + b
>>>
>>> In the first line a is assigned 0 and b is assigned 1 simultaneously.
>>>
>>> However what is the sequence of operation in the second statement? I;m
>>> confused due to the inter-dependence of the variables.
>>
>> The expressions of the right of the assignment operator are evaluated
>> before assigning any new values, to the destinations on the left side
>> of the assignment operator.
>> So substitutig the old values of a and b the second assignment means
>>
>> a, b = 0, 0 + 1
>>
>> Simplifying the Python Reference Manual ("6.3 Assignment Statements")
>> a little :
>>
>> assignment_stmt ::= target_list "="+ expression_list
>>
>> An assignment statement evaluates the expression list (remember that
>> this can be a single expression or a comma-separated list, the latter
>> yielding a tuple) and assigns the single resulting object to each of
>> the target lists, from left to right.
>>
>> [...]
>>
>> WARNING: Although the definition of assignment implies that overlaps
>> between the left-hand side and the right-hand side are `safe' (for
>> example "a, b = b, a" swaps two variables), overlaps within the
>> collection of assigned-to variables are not safe! For instance, the
>> following program prints "[0, 2]":
>>
>> x = [0, 1]
>> i = 0
>> i, x[i] = 1, 2
>> print x
>>
>> Lorenzo Gatti
>
> Thank you for the explanation. I guess my question can be simplified as:
>
> First step: a, b = 0, 1
> No problem here as a and b are assigned values.
>
> Second step: a, b = b, a + b
>
> Now my question is does b become a + b after a becomes 1 or while a
> stays at 0?
>
> As the assignment occurs simultaneously I suppose the answer is while a
> stays at 0.

Read the previous response carefully and you'll answer your question. The
right hand side is EVALUATED in full before values are assignated to the
left hand side. Evaluating b, a+b results in 1, 1. The, those values are
assigned to a, b.

--
Gabriel Genellina

Aaron Brady

3/2/2008 11:15:00 AM

0

On Mar 2, 4:49 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar> wrote:
> En Sun, 02 Mar 2008 08:25:49 -0200, Schizoid Man <sc...@lon.don> escribi?:
>
>
>
>
>
> > Lorenzo Gatti wrote:
> >> On Mar 1, 3:39 pm, Schizoid Man <sc...@lon.don> wrote:
> >>> As in variable assignment, not homework assignment! :)
>
> >>> I understand the first line but not the second of the following code:
>
> >>> a, b = 0, 1
> >>> a, b = b, a + b
>
> >>> In the first line a is assigned 0 and b is assigned 1 simultaneously.
>
> >>> However what is the sequence of operation in the second statement? I;m
> >>> confused due to the inter-dependence of the variables.
>
> >> The expressions of the right of the assignment operator are evaluated
> >> before assigning any new values, to the destinations on the left side
> >> of the assignment operator.
> >> So substitutig the old values of a and b the second assignment means
>
> >> a, b = 0, 0 + 1
>
> >> Simplifying the Python Reference Manual ("6.3 Assignment Statements")
> >> a little :
>
> >> assignment_stmt ::= target_list "="+ expression_list
>
> >> An assignment statement evaluates the expression list (remember that
> >> this can be a single expression or a comma-separated list, the latter
> >> yielding a tuple) and assigns the single resulting object to each of
> >> the target lists, from left to right.
>
> >> [...]
>
> >> WARNING: Although the definition of assignment implies that overlaps
> >> between the left-hand side and the right-hand side are `safe' (for
> >> example "a, b = b, a" swaps two variables), overlaps within the
> >> collection of assigned-to variables are not safe! For instance, the
> >> following program prints "[0, 2]":
>
> >> x = [0, 1]
> >> i = 0
> >> i, x[i] = 1, 2
> >> print x
>
> >> Lorenzo Gatti
>
> > Thank you for the explanation. I guess my question can be simplified as:
>
> > First step: a, b = 0, 1
> > No problem here as a and b are assigned values.
>
> > Second step: a, b = b, a + b
>
> > Now my question is does b become a + b after a becomes 1 or while a
> > stays at 0?
>
> > As the assignment occurs simultaneously I suppose the answer is while a
> > stays at 0.
>
> Read the previous response carefully and you'll answer your question. The  
> right hand side is EVALUATED in full before values are assignated to the  
> left hand side. Evaluating b, a+b results in 1, 1. The, those values are  
> assigned to a, b.
>
> --
> Gabriel Genellina- Hide quoted text -
>
> - Show quoted text -

Another way to think of it is:

a, b= b, a+b

--->

X= b, a+b
a, b= X

where X is a pair (2-tuple, two-element tuple, ordered pair, &c.)

Schizoid Man

3/2/2008 7:14:00 PM

0

Gabriel Genellina wrote:
> En Sun, 02 Mar 2008 08:25:49 -0200, Schizoid Man <schiz@lon.don> escribi�:
>
>> Lorenzo Gatti wrote:
>>> On Mar 1, 3:39 pm, Schizoid Man <sc...@lon.don> wrote:
>>>> As in variable assignment, not homework assignment! :)
>>>>
>>>> I understand the first line but not the second of the following code:
>>>>
>>>> a, b = 0, 1
>>>> a, b = b, a + b
>>>>
>>>> In the first line a is assigned 0 and b is assigned 1 simultaneously.
>>>>
>>>> However what is the sequence of operation in the second statement? I;m
>>>> confused due to the inter-dependence of the variables.
>>>
>>> The expressions of the right of the assignment operator are evaluated
>>> before assigning any new values, to the destinations on the left side
>>> of the assignment operator.
>>> So substitutig the old values of a and b the second assignment means
>>>
>>> a, b = 0, 0 + 1
>>>
>>> Simplifying the Python Reference Manual ("6.3 Assignment Statements")
>>> a little :
>>>
>>> assignment_stmt ::= target_list "="+ expression_list
>>>
>>> An assignment statement evaluates the expression list (remember that
>>> this can be a single expression or a comma-separated list, the latter
>>> yielding a tuple) and assigns the single resulting object to each of
>>> the target lists, from left to right.
>>>
>>> [...]
>>>
>>> WARNING: Although the definition of assignment implies that overlaps
>>> between the left-hand side and the right-hand side are `safe' (for
>>> example "a, b = b, a" swaps two variables), overlaps within the
>>> collection of assigned-to variables are not safe! For instance, the
>>> following program prints "[0, 2]":
>>>
>>> x = [0, 1]
>>> i = 0
>>> i, x[i] = 1, 2
>>> print x
>>>
>>> Lorenzo Gatti
>>
>> Thank you for the explanation. I guess my question can be simplified as:
>>
>> First step: a, b = 0, 1
>> No problem here as a and b are assigned values.
>>
>> Second step: a, b = b, a + b
>>
>> Now my question is does b become a + b after a becomes 1 or while a
>> stays at 0?
>>
>> As the assignment occurs simultaneously I suppose the answer is while a
>> stays at 0.
>
> Read the previous response carefully and you'll answer your question.
> The right hand side is EVALUATED in full before values are assignated to
> the left hand side. Evaluating b, a+b results in 1, 1. The, those values
> are assigned to a, b.

Thank you very much. It's clear now.