set variable to looping index?

Dave Angel davea at
Wed Jul 29 23:47:43 CEST 2009

Martin wrote:
> Hi,
> I am trying to set the return value from a function to a name which I
> grab from the for loop. I can't work out how I can do this without
> using an if statement...
> for f in var1_fn, var2_fn, var3_fn:
>     if f.split('.')[0] == 'var1':
>         var1 = call_some_function(f)
> 	.
>         .
>         .
>       etc
>  Really I would like to remove the need for this if loop and I am sure
> there is a simple way I am missing?
> Many thanks
> Martin

Is this a real problem, or is it a "programming puzzle"?   If it's the 
latter, maybe someone else can help.

But if it's a real problem, give us some context, and maybe we can 
figure out how to help.

If I took this fragment at face value, I'd simply replace it by:

var1 = call_some_function(var1_fn)
var2 = call_some_function(var2_fn)
var3 = call_some_function(var3_fn)

Is this fragment part of a function definition, or is it top-level?  Are 
there really exactly three var*_fn objects, or might there be an 
arbitrary number of them?   Do you want to tell us the types of these 
three objects?  Is there content really tied directly to their name?  
Did they get their values from literals, or were they computed at some 
other point?


More information about the Python-list mailing list