set variable to looping index?

M

Martin

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
 
P

Peter Otten

Martin said:
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?

Use dictionaries:

functions = {"var1": some_function, "var2": some_other_function, ...}
return_values = {}

for arg in var1_fn, var2_fn, var3_fn:
key = arg.split(".")[0]
return_values[key] = functions[key](arg)

Peter
 
B

Bearophile

Martin:
for f in var1_fn, var2_fn, var3_fn:
    if f.split('.')[0] == 'var1':
        var1 = call_some_function(f)
        .
        .
        .
      etc

As usual I have big problems in understanding what you want to do.

Please, show an example of the contents of var1_fn, var2_fn, etc.

Generally you can create a dict:
results = {}

And then fill it with name-result pairs:
name = value.split('_')[0]
results[name] = some_function(value)

Bye,
bearophile
 
D

Dave Angel

Martin said:
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?


DaveA
 
R

Rhodri James

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())
 
M

Martin

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())

Hi all,

Thanks and apologises I wasn't trying to be cryptic, like all things
when I wrote it I thought it was quite transparent.

All I was trying to do was call a function and return the result to an
a variable. I could admittedly of just done...

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

where var1_fn, var2_fn, var3_fn are just filenames, e.g. var1_fn =
'x.txt'. But I figured I would try and make it slightly more generic
whilst I was at it, hence my attempt to use the filenames to create
the variable names (hence the loop). I will as suggested try the
dictionary option. I appreciate all the suggestions.

Thanks

Martin
 
J

Jan Kaliszewski

30-07-2009 said:
All I was trying to do was call a function and return the result to an
a variable. I could admittedly of just done...

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

where var1_fn, var2_fn, var3_fn are just filenames, e.g. var1_fn =
'x.txt'. But I figured I would try and make it slightly more generic
whilst I was at it, hence my attempt to use the filenames to create
the variable names (hence the loop).

Hi,

Then you could also consider using simply lists:

filenames = p'foo', 'bar', baz']
results = []
for name in filenames:
results.append(some_function(name))

If filenames were generated according to a particular pattern, you can
mimic that pattern and generate filenames list using
list-comprehension, e.g.:

filenames = ['file{nr}.txt'.format(nr=nr) for nr in range(13)]

Chreers,

*j
 
M

Martin

30-07-2009 said:
All I was trying to do was call a function and return the result to an
a variable. I could admittedly of just done...
var1 = some_function(var1_fn)
var2 = some_function(var2_fn)
var3 = some_function(var3_fn)
where var1_fn, var2_fn, var3_fn are just filenames, e.g. var1_fn =
'x.txt'. But I figured I would try and make it slightly more generic
whilst I was at it, hence my attempt to use the filenames to create
the variable names (hence the loop).

Hi,

Then you could also consider using simply lists:

filenames = p'foo', 'bar', baz']
results = []
for name in filenames:
     results.append(some_function(name))

If filenames were generated according to a particular pattern, you can
mimic that pattern and generate filenames list using
list-comprehension, e.g.:

filenames = ['file{nr}.txt'.format(nr=nr) for nr in range(13)]

Chreers,

*j

I guess I wanted to keep the function returns in separate arrays in
this case, hence my attempt to make variable names based on the
filenames.

Thanks

Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,184
Latest member
ZNOChrista

Latest Threads

Top