1

in Python: may be so obvious, :) anyway, we are looking for ?, below:

def printname(x):
   print ?,x

>>> a = 1.3
>>> printname(a)
>>> 'a',1.3

so something instead of ? to represent the name of passed argument.
if not that obvious ? any trick or solution?

1
  • as @gnibbler demonstrates, the exact solution your asking is approximately possible, but it's unlikely to be genuinely useful, and even more so, will probably be unreliable. What problem are you really trying to solve, maybe we can suggest an alternate approach to the one you're asking for. Commented May 24, 2013 at 12:44

2 Answers 2

2

You can't possibly know this. Objects don't know their names. They can have many names, or none at all, if they're simply an item in another object or list.

Sign up to request clarification or add additional context in comments.

1 Comment

As you see in the tiny link in the post blue(?) we noticed it may not be possible. However, we still are hopeful for some trick to make our function to work. Think this way. Any idea?
1

It's not impossible. You need to traverse back up stack frames and find the line of code that calls the function.

It's an ok hack to do for debugging, but no way I would use it in real code

Here's a starting point for you:

import inspect
def printname(x):
    print inspect.stack()[1][4]

a = 1.3
printname(a)

1 Comment

Great! it prints: ['printname(a)\n'] which can be easily used not only for extraction of the name of the argument (a) but also the function's name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.