set variable to looping index?

Rhodri James rhodri at wildebst.demon.co.uk
Wed Jul 29 18:02:00 EDT 2009


On Wed, 29 Jul 2009 19:56:28 +0100, Martin <mdekauwe at gmail.com> 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?

It's a little hard to tell what you actually want from your description,
but it looks like you're fighting the language unnecessarily here.  If
you have a sequence of functions that you want a sequence of results
out of, you should be thinking in terms of a sequence type.  A list,
in other words.

results = []
for f in fn1, fn2, fn3:
     results.append(f())


-- 
Rhodri James *-* Wildebeest Herder to the Masses



More information about the Python-list mailing list