set variable to looping index?

Discussion in 'Python' started by Martin, Jul 29, 2009.

  1. Martin

    Martin Guest

    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
     
    Martin, Jul 29, 2009
    #1
    1. Advertisements

  2. Martin

    Peter Otten Guest

    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
     
    Peter Otten, Jul 29, 2009
    #2
    1. Advertisements

  3. Martin

    Bearophile Guest

    Martin:
    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
     
    Bearophile, Jul 29, 2009
    #3
  4. Martin

    Dave Angel Guest

    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
     
    Dave Angel, Jul 29, 2009
    #4
  5. Martin

    Rhodri James Guest

    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, Jul 29, 2009
    #5
  6. Martin

    Martin Guest

    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
     
    Martin, Jul 29, 2009
    #6
  7. 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
     
    Jan Kaliszewski, Jul 29, 2009
    #7
  8. Sorry, should be of course:

    filenames = ['foo', 'bar', baz']

    *j
     
    Jan Kaliszewski, Jul 30, 2009
    #8
  9. Martin

    Martin Guest

    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
     
    Martin, Jul 30, 2009
    #9
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.