1

In bash you can use $@ or $* to get a list of arguments of the function/method as a list. Does python have something similar? I'm trying to do a sort of logging where the log for a function will print out the function arguments.

Example

def foo(a: str, b: List) 
    print(magic_get_args_function());
    ....

foo("h", [1, 2, 3])

Output

h, [1, 2, 3]    # or even include the argument names if possible
SwimMaster
  • 351
  • 3
  • 17
  • Aside: `$*` does not return arguments *as a list*, as the first sentence of this question claims; it coerces them to a single string (which then, if used unquoted, gets split back into a list and glob-expanded, but isn't guaranteed to be the same as the _original_ argument list). Always use `"$@"` (with the quotes) instead. – Charles Duffy Jun 16 '20 at 18:17
  • @CharlesDuffy thanks for the clarification – SwimMaster Jun 17 '20 at 20:06

1 Answers1

0

How about this ?

from inspect import getcallargs

def foo(a: str, b: list):
    print(getcallargs(foo, a, b))

foo("h", [1, 2, 3])
{'a': 'h', 'b': [1, 2, 3]}
Jason Yang
  • 11,284
  • 2
  • 9
  • 23