[Tutor] scope question

Poor Yorick gp@pooryorick.com
Wed Jan 15 17:24:01 2003


Norvell Spearman wrote:

>
>def increment(time, seconds):
>    time = addTime(time, makeTime(seconds))
>
>[snip]
>
>
>Yes and not exactly.  When I call increment from either the program or
>from the interactive Python shell, it doesn't change the time object
>passed to it --- which is consistent with your example function. 
>

In Python, paramaters are always passed into function by reference. 
 However, once you are inside the function, and type

time = (something)

You create a new identifier in the local scope which hides the "time" 
identifier which was passed in by reference.

>
>Now if I do something like this
>
>def increment(time, seconds):
>    newTime = addTime(time, makeTime(seconds))
>    time.hours = newTime.hours
>    time.minutes = newTime.minutes
>    time.seconds = newTime.seconds
>

In this case, you are changing the values of identifies inside the scope 
of the "time" object which you passed into the function.  You still have 
access to the "time" object because you didn't reassign the "time" 
identifier within the function.


Poor Yorick
gp@pooryorick.com