-1

When a function is called, I would like to print the names and values of it's parameters. Something like:

>>> def foo(bar, baz):
>>>    print_func_params() 

>>>foo(5, 'test')
>>>bar=5, baz='test'

Right now I do it manually:

print(f"bar={bar},baz={baz}")

But this is a pain because as new parameters get added there's always the risk of missing something.

For context, this code is used for debugging a hardware interface. When the hardware isn't present it prints the IO instead of sending it to the hardware. This code lets me see the full sequence of commands that are being sent without needing the hardware present.

I found this answer which can work for single variables when you know what they are (and uses eval). I'd really like to avoid eval and be able to do it for an unknown set of parameters.

Viglione
  • 4,942
  • 2
  • 21
  • 45
  • 3
    Does this answer your question? [How to get method parameter names?](https://stackoverflow.com/questions/218616/how-to-get-method-parameter-names) – Olvin Roght Jun 12 '20 at 22:08
  • May I ask *why*? In any case, on possibility is to replace explicit parameters with `**kwargs` which will return a dict – juanpa.arrivillaga Jun 12 '20 at 22:13
  • @OlvinRoght That's fine for getting the names, but how would I then get the values? Use `eval`? – Viglione Jun 12 '20 at 22:14
  • 1
    @MatthewSalvatoreViglione, there's link to [`inspect`](https://docs.python.org/3/library/inspect.html) module docs, you can find there function which fits your requirements better. Combination of [`inspect.currentframe()`](https://docs.python.org/3/library/inspect.html#inspect.currentframe) and [`inspect.getargvalues()`](https://docs.python.org/3/library/inspect.html#inspect.getargvalues) should work. There's also [`inspect.formatargvalues()`](https://docs.python.org/3/library/inspect.html#inspect.formatargvalues) which could be even better. – Olvin Roght Jun 12 '20 at 22:17

2 Answers2

2

After you've add explanation how you want to apply this, I think best way will be to use decorator. It's more universal solution, because you can add it to any function in your code and it will print all debug info if debug mode is on.

Code:

from functools import wraps

DEBUG = True

def debug_log(function):
    @wraps(function)
    def wrapper(*args, **kwargs):
        if DEBUG:
            print(">> Called", function.__name__, "\n",
                {**dict(zip(function.__code__.co_varnames, args)), **kwargs})
        result = function(*args, **kwargs)
        if DEBUG:
            print(">>", function.__name__, "return:\n", result)
        return result
    return wrapper

@debug_log
def first_example(a, b, c):
    return 100

@debug_log
def second_example(d, e, f):
    return 200

first_example(10, 11, 12)
first_example(c=12, a=10, b=11)
second_example(13, 14, 15)
second_example(e=14, d=13, f=15)
DEBUG = False
first_example(0, 0, 0)
second_example(1, 1, 1)

Output:

>> Called first_example 
 {'a': 10, 'b': 11, 'c': 12}
>> first_example return:
 100
>> Called first_example 
 {'c': 12, 'a': 10, 'b': 11}
>> first_example return:
 100
>> Called second_example 
 {'d': 13, 'e': 14, 'f': 15}
>> second_example return:
 200
>> Called second_example 
 {'e': 14, 'd': 13, 'f': 15}
>> second_example return:
 200
Olvin Roght
  • 6,675
  • 2
  • 14
  • 32
  • Is there a way to get the name of a parameter if it was called without a keyword? I'm thinking using `inspect` like you mentioned earlier may be a good way – Viglione Jun 12 '20 at 23:22
  • @MatthewSalvatoreViglione, you can use hack from MrNobody33's answer. – Olvin Roght Jun 12 '20 at 23:25
1

You could try to use func.__code__.co_varnames like this:

def foo(bar, baz):
    magic_parameter_printing() 

bar=0
baz=None  
for var in foo.__code__.co_varnames:
    print(var,'= ',eval(var))

Output:

bar=0
baz=None 
MrNobody33
  • 6,143
  • 5
  • 18