473,326 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Missing Something Simple

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
Jul 21 '05 #1
2 906
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/
Jul 21 '05 #2
On Tue, 12 Jul 2005 14:44:00 +0100, John Abel <ja***@plus.net> declaimed
the following in comp.lang.python:
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[i] = 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.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: eltronic | last post by:
here is some code to add right click context menus. in the few apps I've edited sofar, it just works, once you run rCbinder with the root of the gui or bind B3 all present and future widgets will...
102
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is missing. The response was simply because the compiler...
0
by: A.Black | last post by:
Hi, Trying to create a simple application in C# that requires access to Outlooks mail folders. Have downloaded sample code and that works fine however when I try to create my own project & code I...
2
by: Andrew | last post by:
Hey all, Have a strange one here, and being still fairly new to .NET isn't helping me understand it. I am having a problem where a DataReader doesn't return all the rows when I try to use a...
5
by: unwantedspam | last post by:
Hi All, What am I missing. I have trying to create a simple web based application. Basically there are three textboxes and two buttons (Save, Reset). I assume the code behind the save button...
17
by: Justin Emlay | last post by:
I'm hopping someone can help me out on a payroll project I need to implement. To start we are dealing with payroll periods. So we are dealing with an exact 10 days (Monday - Friday, 2 weeks). ...
1
by: benclissen | last post by:
Hi, I want to do something simple read an xml and print out just 1 value. Now everytime I do this I got this "Root elements are missing" exception. I try it by using xmlreader, dataset or...
0
by: Jigar.Patel | last post by:
I have simple remoting server exposing following simple method. When I try to add webreference to this server in another project, it gives me following error: Custom tool error: Unable to import...
1
by: chadw | last post by:
Not sure what I am missing sure its probably something very simple, I need something to get my entered text out of the textbox and entered into a database currently the code looks like this. ...
12
by: The Frog | last post by:
Hi all, Does anyone have a way to print forms at design time directly from the IDE? I am not a great fan of the print screen to paint to print method. I would really like to know if anyone has a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.