[Tutor] first steps (variable, assignment)

Gregor Lingl glingl at aon.at
Fri Apr 2 03:38:18 EST 2004



Karl Pflästerer schrieb:

>>Here is another example of variable usage:
>>    
>>
>>a = 1
>>print a
>>a = a + 1
>>    
>>
>[...]
>
>You uses the right words.  If you assign a value you have a left hand
>side (lhs) and a right hand side (rhs).  First the interpreter evaluates
>the rhs and after that it assigns the returned value to the lhs.
>
>So if you write
>   a      =   a + 1
>   ^          ^
>   |          |  
>   lhs        rhs
>
>the interprter first evaluates a+1, stores that value somewhere and
>assigns then a to that value.  From now on the old value of a is
>forgotten.
>
>  
>
Hi Karl!

As assignment is a very important topic, I'd like to comment your 
explanation. It uses the
word "assign to"  in two different ways, which reflect two different 
views at assignment.
The first one is the traditional one,  a value is assigned to a 
"variable"  (or a "variable name").
In my experience it is the by far more  popular way to look at 
"assignment" in many tutorials,
programming courses, cs-intro-books...

In the second part of your explanation you say  that  a  - the  variable 
name  -  is assigned to
the object just constructed and stored somewhere. That's the other way 
round.
When programming with Python, I think, this is the by far more useful 
(and correct)
view on the assignment statement.

Simply think on Python producing (constructing)  things (objects (e. g. 
numbers, strings, lists,
trutles, httpservers etc. ...))  and  if you want to use them (more than 
once) you have to name
them.

a = 1   # object 1 (an int) gets name a

b = a   # object named a gets a second name b

a = a + 1  # new object a+1 is constructed and gets the name a

So now name a is used for a  different thing,  but in this example  
object 1
("the old value") is not  forgotten as its second name b remains  
assigned to it. 

imho this view at the assignment statement in conjunction with the 
notion of
mutable/immutable objects (not explained here) easily opens the whole
universe of Python programming.

Regards, Gregor

P. S.: For my part, I'd like to consider the concept of "variable" as 
deprecated
and to replace it entirely by the concepts "name" and "object".





More information about the Tutor mailing list