scope of function parameters

Christopher Head chead at is.invalid
Sun May 29 17:48:53 EDT 2011


On Sun, 29 May 2011 16:19:11 -0400
Henry Olders <henry.olders at mcgill.ca> wrote:

> > def fnc2(c):
> > 		c = c[:]
> >        c[1] = 'having'
> >        return c
> 
> Thank you, Wolfgang. That certainly works, but to me it is still a
> workaround to deal with the consequence of a particular decision.
> From my perspective, a function parameter should be considered as
> having been assigned (although the exact assignment will not be known
> until runtime), and as an assigned variable, it should be considered
> local.
> 
> Henry

This has nothing to do with function parameters and everything to do
with what a "variable name" actually means. You can get the same effect
with only globals:

>>> x=[1,2,3]
>>> y=x
>>> x.append(7)
>>> y
[1, 2, 3, 7]

Why in the world does "y" end with 7, even though it was appended to
"x"? Simple: because "x" and "y" are two names for the same list, as
Henry explained. No functions involved, no locals, no parameters, no
scoping. Again, if "y=x" were instead "y=x[:]" then the output would be
"[1,2,3]" because "y" would refer to a copy of the list rather than the
same list.

Chris



More information about the Python-list mailing list