parameter passing question

M

Micah

Python gurus:

No, this isn't about pass-by-value vs pass-by-reference, this is more along
the lines of functional programming.

Basically, I want to be able to call a function on a list of arguments
without knowing the syntax of the function. I guess a code example would be
best:

-------
def foo(arg1, arg2):
# Do something with arg1 and arg2

def main():
f = foo
args = ["hello", "world"]
# Want to call foo("hello", "world") using variable f and list args
--------

So, what I'm trying to do is call f(args[0], args[1]). However, I want to
be able to do it with any length argument list. Given any function f and a
list of arguments args, I want to be able to call f(args[0], args[1], ...,
args[n])

Any suggestions?

--
=================================
Micah Z. Wedemeyer
Research Scientist I, ELSYS
Georgia Tech Research Institute
Atlanta, GA 30332
678.428.1283
=================================
 
M

Mathias Waack

Micah said:
Python gurus:

It can't be a guru question just because I know the answer;)
So, what I'm trying to do is call f(args[0], args[1]). However, I
want to
be able to do it with any length argument list. Given any function
f and a list of arguments args, I want to be able to call
f(args[0], args[1], ..., args[n])

If args is a list:
.... return f(*args)

So you can call for instance:

c(max,[1,2,3,4])

Or a function with an arbitrary number of arguments:
.... return f(*args)

So you can call it as

c(max,1,2,3,4,5)

Mathias
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top