Missing Something Simple

J

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.

I'm hoping someone can point me in the right direction.

John
 
T

Thomas Guettler

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="foo"

# List was changed
print varList # --> ["foo", "foo", "foo"]


HTH,
Thomas
 
D

Dennis Lee Bieber

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 ]
varOne (Two, Three...) are /references/ to something located in
memory. Think of them as "Post-It" notes, with the names (varOne, etc),
stuck to boxes inside a bigger box (the bigger box has a Post-It
"varList"). {Well, it might be closer to say that the "varList" box has
a sheet of paper inside with the names "varOne", etc. on it, and you
have to /then/ look for the box with the "varOne" Post-It}
for indivVar in varList:

indivVar is a reference to an element in the list... IE, another
Post-It note with the name indivVar that is now stuck to one of the
boxes (the box now has TWO Post-It notes -- say varOne and indivVar).
indivVar = returnVarFromFunction()
You now have another box that is "returnVarFromFunction". You
have MOVED the Post-It "indivVar" from the original box to the new box.
You have made NO changes inside the box labeled "varList".


Using

for i in range(len(varList)):
varList = returnVarFromFunction()

goes into the "varList" box, draws a line through the "i"th name on the
list, and puts in a temp name (reference) for the
"returnVarFromFunction". Note that the box with "varOne" is still out in
the room, but the list inside the box "varList" doesn't reference it, it
now references the returned value.

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top