Newbie Q: var params
Andrew Dalke
dalke at acm.org
Tue Apr 10 23:59:10 EDT 2001
zzzzz wrote:
>It looks like "parameter=[2,4,6,8]" creates a new local variable that
>overrides the passed parameter. How do I do this properly so that the
>passed parameter changes in all parameter type cases?
You don't have the right internal model for how Python passes
information about. Everything is "by reference" so when
you say:
>>> def paramtest(parameter):
you are creating a new local variable which is a reference to
the data passed in to it.
Later on when you say
parameter=[2,4,6,8]
you use the same local variable, but this time it's a reference
to a new object.
To do what you want it to do you would have to tell the
original object (passed in by reference) to update itself with
new data. That will differ depending on the object - and some
objects are immutable, meaning they cannot be changed.
For lists this is done as
parameter[:] = [2,4,6,8]
which says to replace the complete list ([:] means "starting from
the beginning and going to the end") with the values in [2,4,6,8].
Andrew
dalke at acm.org
More information about the Python-list
mailing list