Totally Confused: Passing variables to functions

Michael Mayhew mayhew at cis.ohio-state.edu
Thu Jun 5 04:08:25 EDT 2003


Howdy,

    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
blah()
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,

Michael








More information about the Python-list mailing list