[Tutor] scope question

Magnus Lycka magnus@thinkware.se
Wed Jan 15 12:31:01 2003


At 08:58 2003-01-15 -0600, Norvell Spearman wrote:
>My question (finally) is this:  Why doesn't increment alter t1 while
>``t1 = addTime(t1, makeTime(34))'' in an interactive session does alter
>t1?

Believe it or not, but this has little to do with
scopes... I haven't seen the definition of the
increment function, but may I imagine that it contains
an assignment to the parameter variable.?

Parameter passing is like assignment. Roughly...

def p(x):
     x = x + " world"
     print x

greeting = "Hello"
p(greeting)
print greeting

...is basically the same thing as...

greeting = "Hello"
x = greeting
x = x + "world"
print x
print greeting

...at least as far as your problem is concerned.
Does that make sense? Does it explain what happens
to your code?


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se