Missing Something Simple
Thomas Guettler
guettli at thomas-guettler.de
Tue Jul 12 10:12:19 EDT 2005
Am Tue, 12 Jul 2005 14:44:00 +0100 schrieb John Abel:
> Hi,
>
> I have a list of variables, which I am iterating over. I need to set
> the value of each variable. My code looks like:
>
> varList = [ varOne, varTwo, varThree, varFour ]
>
> for indivVar in varList:
> indivVar = returnVarFromFunction()
>
> However, none of the variables in the list are being set. I thought of
> using setattr, but this code sits in a function, and not class, so I'm
> unsure what the object would be.
Hi,
"indivVar" is a *reference* to a value. You only change the
reference, not the value. Maybe this code helps you:
a="a"
b="b"
c="c"
varList = [a, b, c]
for x in varList:
x = "foo"
# nothing changed
print varList # --> ["a", "b", "c"]
for i in range(len(varList)):
varList[i]="foo"
# List was changed
print varList # --> ["foo", "foo", "foo"]
HTH,
Thomas
--
Thomas Güttler, http://www.thomas-guettler.de/
More information about the Python-list
mailing list