Is there a way to get the function parameters in the callee as the caller put it?

Viewed 85

I want to achieve that calling foo(2*3) prints 2*3.

The reason is that I try to create a test framework for querying data files and I want to print the query statement with the assertion result.

I tried to get it work via the inspect module but I could not make it work.

2 Answers

In general, the answer is "no", since the value received by the function is the result of the expression 2*3, not the expression itself. However, in Python almost anything is possible if you really want it ;-)

For simple cases you could achieve this using the inspect module like this:

#!/usr/bin/env python3

import inspect


def foo(x):
    context = inspect.stack()[1].code_context[0]
    print(context)


def main():
    foo(2 * 3)


if __name__ == '__main__':
    main()

This will print:

    foo(2 * 3)

It would be trivial to get the part 2 * 3 from this string using a regular expression. However, if the function call is not on a single line, or if the line contains multiple statements, this simple trick will not work properly. It might be possible with more advanced coding, but I guess your use case is to simply print the expression in a test report or something like that? If so, this solution might be just good enough.

Because the expression is evaluated before it is passed to the function, it is not possible to print out the un-evaluated expression.

However, there is a possible workaround. You can instead pass the expression as a string and evaluate it inside the function using eval(). As a simple example:

def foo(expr):
    print(expr)
    return(eval(expr))

Please note however that using eval is considered bad practice.

A better solution is to simply pass a string as well as the expression, such as foo(2*3, "2*3").

Related