Totally Confused: Passing variables to functions

Michael Mayhew mayhew at
Thu Jun 5 10:08:25 CEST 2003


    To address the first problem, the following sample worked for me:

def blah(arg):
     arg[:] = []
     return arg

I think the reason is that using the slice operation, you are still, as you
noticed, using methods on the "object reference" and so can change the
mutable object in place.

For the second example, this sounds like a question of scope as v is
declared in your global scope, but arg is local to the function. If you
wanted to change v itself in the function blah, you would write

def blah():
    global v
    v = v + 1

v = 1
Now v equals 2!

If you wanted to simply change the local function reference to v, you would
have to return the result and assign it to v, like so:

def blah(arg):
    arg = arg+1
    return arg

v = blah(v)
Now v equals 2!

I hope this helps somewhat,


More information about the Python-list mailing list