Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #6569 > unrolled thread

Re: scope of function parameters

Started byHenry Olders <henry.olders@mcgill.ca>
First post2011-05-29 16:19 -0400
Last post2011-05-29 14:48 -0700
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: scope of function parameters Henry Olders <henry.olders@mcgill.ca> - 2011-05-29 16:19 -0400
    Re: scope of function parameters Christopher Head <chead@is.invalid> - 2011-05-29 14:48 -0700

#6569 — Re: scope of function parameters

FromHenry Olders <henry.olders@mcgill.ca>
Date2011-05-29 16:19 -0400
SubjectRe: scope of function parameters
Message-ID<mailman.2237.1306700356.9059.python-list@python.org>
Henry




On 2011-05-29, at 5:47 , Wolfgang Rohdewald wrote:

> On Sonntag 29 Mai 2011, Henry Olders wrote:
>> It seems that in Python, a variable inside a function is
>> global unless it's assigned.
> 
> no, they are local
> 
>> I would have thought that a function parameter would
>> automatically be considered local to the function. It doesn't
>> make sense to me to pass a global to a function as a
>> parameter.
> 
> it is local. But consider what you actually passed:
> You did not pass a copy of the list but the list itself.
> You could also say you passed a reference to the list.
> All python variables only hold a pointer (the id) to
> an object. This object has a reference count and is
> automatically deleted when there are no more references
> to it.
> 
> If you want a local copy of the list you can either
> do what you called being ugly or do just that within
> the function itself - which I think is cleaner and
> only required once.
> 
> 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

[toc] | [next] | [standalone]


#6585

FromChristopher Head <chead@is.invalid>
Date2011-05-29 14:48 -0700
Message-ID<20110529144853.2adff584@kruskal.chead>
In reply to#6569
On Sun, 29 May 2011 16:19:11 -0400
Henry Olders <henry.olders@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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web