1

Is it possible in Python to split a function call up into the function and a list of its arguments? For example, taking func(1,4,True) and turning it into [func, 1, 4, True] or something similar. Ideally, I'd like to do this for an arbitrary function.

EDIT: I am a TA for a programming course, and the purpose of this is to check what test cases the students include in their code. I want to see what values the student has called their code with in order to make sure they're testing it properly. We have the students use a comparison function (which they import from a module) when writing their tests, so I'm hoping to edit the comparison function in order to check which test cases they've included.

I've had some other ideas on how to achieve the same end-result (such as reading the code in as text or copying the student code to a new file with a different definition of func), but both would involve more work and have more potential for problems than redefining the comparison function.

2
  • 2
    Can you explain in more detail exactly what you want to do? I don't understand the question. A function call isn't an object so it doesn't make terribly much sense to talk about splitting it up, as far as I can see. Commented Nov 15, 2012 at 22:41
  • At parse time or execution time? Commented Nov 16, 2012 at 0:08

3 Answers 3

3

The direct answer to your question is no, since a function call is not an object, but a statement.

To get information about any function such as its name, its arguments, etc. you can use some properties and methods that are attached to each function:

>>> def foo(a,b):
...    print a,b
... 
>>> foo.func_name
'foo'
>>> foo.func_code.co_varnames
('a', 'b')

Typing dir(funcname) will give you more things to explore.

If you want to know how a function is working, how arguments are assigned when called, and for other such investigative troubleshooting - there is the inspect module.

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

Comments

2

How about defining a decorator:

def func_call_decorator(func):
    def _wrapper(*args, **kwargs):
        ret = func(*args, **kwargs)
        return [func]+list(args)+kwargs.items() + [ret]
    return _wrapper

@func_call_decorator
def foo(a,b):
    return a+b

print foo(2,3)

which produces:

[<function foo at 0x10045f5f0>, 2, 3, 5]

I also attached the return value of the function for given arguments as the last element of the list.

I wonder what is your use case...

Comments

1
call = [func, 1, 4, True]
f,args = call[0],call[1:]
result = f(*args)

3 Comments

This is the most relevant answer posted so far, but this still doesn't seem to answer the question. The asker said he wants to 'take' func(1,4,True) and 'turn it into' [func, 1, 4, True] (whatever he means by that); what you have done is the other way around.
Hah, I like it. Good one Marcin.
@MarkAmery This reifies the function call.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.