scope of function parameters (take two)

Wolfgang Rohdewald wolfgang at rohdewald.de
Tue May 31 01:13:24 EDT 2011


On Dienstag 31 Mai 2011, Henry Olders wrote:
> What I would like is that the variables which are included in
> the function definition's parameter list, would be always
> treated as local to that function (and of course, accessible
> to nested functions) but NOT global unless explicitly defined
> as global. This would prevent the sort of problems that I
> encountered as described in my original post.

the parameter is local but it points to an object from an outer
scope - that could be the scope of the calling function or maybe
the global scope. So if you change the value of this parameter, 
you change that object from outer scope. But the parameter 
itself is still local. If you do 

def fnc2(c):
   c = 5

the passed object will not be changed, c now points to another
object. This is different from other languages where the "global"
c would change (when passing c by reference)

what you really seem to want is that a function by default
cannot have any side effects (you have a side effect if a
function changes things outside of its local scope). But
that would be a very different language than python

did you read the link Steven gave you?
http://mail.python.org/pipermail/tutor/2010-December/080505.html

-- 
Wolfgang



More information about the Python-list mailing list