1

I have a Python script which creates a dictionary of its own functions and I'd like it to execute them by reading in a function name and arguments using YARP (knowledge of YARP is irrelevant to this question though).

I create a list of strings called "inc" which is populated by values coming into the program. The first item is a function name, and any other strings in the list are arguments. I create a dictionary called "methods" where the key is the function name and the value is a reference to the function object (using the inspect module). I store the return value of the function in a variable "result".

The snippet below shows a simplified version of what I'm using so far, which works fine, but can't handle functions with more than one argument. To circumvent this I use a list if a function needs more parameters:

if len(inc) == 1:
    result = methods[inc[0]]()   # call method with 0 arguments

elif len(inc) == 2:
    result = methods[inc[0]](inc[1])    # call method passing a string

else:
    args = []
    result = methods(inc[0])(inc[1:])    # call method passing a list

Ideally, I'd like to change this so that my functions can have any number of arguments, but I can't figure out how I can do this. I'm new to Python and I have looked at the documentation and various websites - I just can't find a solution. I've tried things like creating a tuple of the arguments, but that doesn't work either as it ends up passing the whole tuple in as one parameter.

Is there a better solution to this problem, like creating some kind of object which represents a set of parameters and passing that into the function? Any suggestions would be greatly appreciated!

2 Answers 2

2

You should check out https://stackoverflow.com/a/3394898/1395668.

The magic you are looking for is the *. Apply this to your list and it unpacks the items into the argument fields of your function:

a = [ 1, 2, 3]
def myfunc(a, b, c):
    return a + b + c

print myfunc(*a)

Check out ** for the same approach for dict

Sign up to request clarification or add additional context in comments.

Comments

0

It's a bit strange to have this kind of mixed structure:

inc = [func_name, arg1, arg2, ...]

Wouldn't it be much more natural to have two separate bits of information?

func_name = ...
args = [arg1, arg2, ...]

The you could do

methods[func_name](*args)

(Usually, I wouldn't bind the functions name to a variable, but preferably the function itself.)

4 Comments

The former approach looks like lisp to me - and nothing wrong with that.
It is strange and that isn't exactly the structure in my program - I put a list like that just to simplify the example. The actual program uses "Bottle" objects used by YARP.
Also, thank you for responding as the * was what I was missing! :-)
@AndrewAlcock: Different programming languages have different idioms, and it usually helps readability to stick to these idioms. In Python, lists are preferably homogeneous.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.