Confused: Newbie Function Calls

Stefan Schwarzer sschwarzer at sschwarzer.net
Mon Aug 30 05:24:34 EDT 2010


Hi Pinku,

On 2010-08-11 21:35, Pinku Surana wrote:
> Even though I used the same name "x" for a local and global variable,
> they are actually completely different. When I call "fun(x)" it COPIES
> the global value of "x" into the local variable "x" in "fun". [...]

The global value isn't copied when calling the function.
Instead, the global and the local name both point to the
same object when the body of the function starts to run.

> def fun(x_local):
>     y_local = 1
>     x_local += y_local
>     print x_local

Only after the assignment "x_local += y_local" x_local
points to a new object which is the result of the addition
of the previously "shared" object and y_local.

Stefan



More information about the Python-list mailing list