0

How to get the names, string literals, of arguments passed to a function, within the function environment?

Pseudo-code:

def myfunc(model, arg_is_list, num):
    # define magical_command that gets args NAMES
    print 'Your passed args are:', magical_command_return
    # use arg_is_list, use model...

Ideal solution:

$mylist={}
$mylist=[1,2,3]
$linear = some_library_model
$myfunc(model='linear', arg_is_list='mylist', num=2)
>Your passed args are: 'linear', 'mylist', '2'  

Any help I can get would be much appreciated. Thanks.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
remi
  • 781
  • 2
  • 13
  • 22

1 Answers1

2

inspect module provides this introspection functionality:

>>> import inspect
>>>
>>> def myfunc(model, arg_is_list, num):
...     print 'Your passed args are:',
...     arg_names = inspect.getargspec(myfunc).args
...     for name in arg_names:
...         print repr(locals()[name]),
... 
>>> myfunc(model='the model', arg_is_list='arrrggg', num=42)
Your passed args are: 'the model' 'arrrggg' 42
wim
  • 338,267
  • 99
  • 616
  • 750
  • @ wim: unfortunately this works only for arg#3: `num=42`. The list-type arg is literally printed with elements values and no name...same thing for `model`: model details but not name – remi Oct 14 '16 at 16:47
  • Why use `inspect`? You can just get the names and values from `locals`. – ekhumoro Oct 14 '16 at 17:03
  • @ekhumoro ...because locals has other names in it too e.g. closure variables. How do you know which ones are arguments and which are not? – wim Oct 14 '16 at 17:05
  • @remi I don't get it - in your question you showed "ideal solution" which only printed the values, not the names. Anyway if you want the names they are there in 'arg_names', use them as you want! – wim Oct 14 '16 at 17:08
  • @wim. Call it at the beginning of the function. The OP is not using a closure, so I don't see the relevance of that. But anyway, your output can be reproduced even more simply with `print('%r, %r, %r' % (model, arg_is_list, num))`. So I really don't see the point in any of this. – ekhumoro Oct 14 '16 at 17:12
  • @ wim: Take the list-type argument for instance. It's `name` is `mylist`, while it `values=[1,2,3]`. The code you suggested prints: `[1,2,3]`. I think my question is not-Pythonically solvable. – remi Oct 14 '16 at 17:16
  • Oh. In your example you passed a string `"mylist"`. Not a list bound like `mylist=[1,2,3]`. You should write a better question, it's confusing :P – wim Oct 14 '16 at 17:21
  • If you're trying to get the name bound to an object, forget it. It's technically possible, however there can be multiple names, or no names at all! – wim Oct 14 '16 at 17:21
  • @wim. FTW: closure variables do not appear in `locals()` when it is called inside a function block. – ekhumoro Oct 14 '16 at 17:25
  • @ekhumoro Forcing it to be the first line of the function is an ugly and unnecessary restriction. I don't know what the point is either but a conceivable use-case could be a self-documenting function, which adapts to changes in the argument names without needing changes in the body of the function? An insane idea, I'll admit, but my point is the OP example has obviously been reduced to a minimal example – wim Oct 14 '16 at 17:26