The following code (gratefully gleaned from elsewhere in stackoverflow):
import inspect
def func(first, second, third):
frame = inspect.currentframe()
# print(f'frame: {inspect.getargvalues(frame)}')
args, _, _, values = inspect.getargvalues(frame)
print(f'function name {inspect.getframeinfo(frame)[2]}')
for i in args:
print(f'{i} = {values[i]}')
return [(i, values[i]) for i in args]
a = 'Test'
b = 'This'
c = 'Now'
func(a, b, c)
prints:
function name func
first = Test
second = This
third = Now
I would like the code to:
- Print a = Test, b = This, c = Now instead of first = Test.. etc
According to No. 1 immediately above, print the arguments names passed to *args. So instead of
def func(first, second, third):
it should bedef func(*args):
whose expected output should be:function name func a = Test b = This b = Now
I am deliberately avoiding using **kwargs (which would solve the problem easily but will require significant change in my code which may not be an option in the immediate term).
P.S. For clarification purposes, I am not looking for a way to get the name of the function (as this is already achieved with the code above) or for a way to get the name of the arguments when the arguments are explicitly named at the function definition. I am only stuck when the arguments are passed via *args (in which case the number or name of arguments cannot be determined until the time the function is being called and the name and the number of arguments are being passed to it)
I am new to Python kindly help!