Missing Something Simple

John Abel jabel at plus.net
Tue Jul 12 10:16:48 EDT 2005


harold fellermann wrote:

>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.
>>    
>>
>
>You only change the value of the local variable in the body
>of the for loop. it has no effect on the list. you could do e.g.
>
>varList = [vorOne,varTwo,varThree,varFour]
>for i in len(varList) :
>	varList[i] = returnVarFromFunction()
>
>However, as in this example the former list values are not used anyway,
>you could just write:
>
>varList = [ returnVarFromFunction for i varList ]
>
>
>cheers,
>
>- harold -
>
>--
>Tages Arbeit, abends Gäste,
>saure Wochen, frohe Feste!
>-- Johann Wolfgang v. Goethe
>
>  
>
The problem I have, is the variables are referenced elsewhere.  They
have been declared before being used in the list.  Basically, I'm after
the Python way of using deferencing.

J




More information about the Python-list mailing list