indirectly addressing vars in Python
Ross
nobody at nospam.noway
Wed Oct 1 10:53:08 EDT 2008
Forgive my newbieness - I want to refer to some variables and indirectly
alter them. Not sure if this is as easy in Python as it is in C.
Say I have three vars: oats, corn, barley
I add them to a list: myList[{oats}, {peas}, {barley}]
Then I want to past that list around and alter one of those values.
That is I want to increment the value of corn:
myList[1] = myList[1] + 1
Is there some means to do that?. Here's my little session trying to
figure this out:
>>> oats = 1
>>> peas = 6
>>> myList=[]
>>> myList
[]
>>> myList.append(oats)
>>> myList
[1]
>>> myList.append(peas)
>>> myList
[1, 6]
>>> myList[1]= myList[1]+1
>>> myList
[1, 7]
>>> peas
6
>>>
So I don't seem to change the value of peas as I wished. I'm passing
the values of the vars into the list, not the vars themselves, as I
would like.
Your guidance appreciated...
Ross.
More information about the Python-list
mailing list