0

let's say I have a function

def argname(nr):
    ...

and I'd like to have this function print the name (str) of the arg passed when calling it, eg

n = 9

argname(n)

this should print "n".

Is this somehow possible in Python 3 ?

  • The function only receives the value, not the external variable the value was held in. – khelwood Aug 09 '20 at 17:50
  • No, it's not posssible for `argname` to know it because your variable `n` is replaced by `9` before even `argname` is called. – Asocia Aug 09 '20 at 17:50
  • You could use the inspect module to look for the place where argname was called and analyse the line of code at that place but that would be painful to implement. – qouify Aug 09 '20 at 18:53
  • thank you for those hints. I will have to find a different approach then. – DonPolettone Aug 09 '20 at 18:54
  • I found something similar that might be able to help you [here.](https://stackoverflow.com/questions/1534504/convert-variable-name-to-string) – Hussein Esmail Aug 10 '20 at 03:21

0 Answers0