How to get the original variable name of variable passed to a function
You can't. It's evaluated before being passed to the function. All you can do is pass it as a string.
Determine original name of variable after its passed to a function
You're right, this is very much impossible in any sane way, since only the value gets passed into the function.
original variable name passed to function?
It's not possible. Even pass-by-reference won't help you. You'll have to pass the name as a second argument.
But what you have asked is most assuredly not a good solution to your problem.
python obtain variable name of argument in a function
You cannot do it like that (as Ignacio Vazquez-Abrams already answered), but you can do it in a similar way:
>>> def foo(**kwargs):
for arg_name in kwargs:
return kwargs[arg_name], arg_name
>>> foo(fib=1)
(1, 'fib')
The only difference is that you must use keyword arguments, otherwise it will not work.
The alternative solution is also to access __name__
attribute of passed variable, which will result in obtaining the name of function, class or name (or anything else that will have this name defined). The only thing that you should be aware of, is that by default this is not the name of the variable, but the original name of the function/class/module (the one assigned when it was being defined). See the example here: http://ideone.com/MzHNND
How to find the name of a variable that was passed to a function?
Not really solution, but may be handy (anyway you have echo('foo')
in question):
def echo(**kwargs):
for name, value in kwargs.items():
print name, value
foo = 7
echo(foo=foo)
UPDATE: Solution for echo(foo)
with inspect
import inspect
import re
def echo(arg):
frame = inspect.currentframe()
try:
context = inspect.getframeinfo(frame.f_back).code_context
caller_lines = ''.join([line.strip() for line in context])
m = re.search(r'echo\s*\((.+?)\)$', caller_lines)
if m:
caller_lines = m.group(1)
print caller_lines, arg
finally:
del frame
foo = 7
bar = 3
baz = 11
echo(foo)
echo(foo + bar)
echo((foo + bar)*baz/(bar+foo))
Output:
foo 7
foo + bar 10
(foo + bar)*baz/(bar+foo) 11
It has the smallest call, but it's sensitive to newlines, e.g.:
echo((foo + bar)*
baz/(bar+foo))
Will print:
baz/(bar+foo)) 11
Related Topics
Does "\D" in Regex Mean a Digit
How to Get a String After a Specific Substring
What Are "First-Class" Objects
How to Get Variable Data from a Class
Pandas: Filter Rows of Dataframe with Operator Chaining
X, = ... - Is This Trailing Comma the Comma Operator
How to Get the Logical Xor of Two Variables in Python
Changing User Agent on Urllib2.Urlopen
Calculating a Directory's Size Using Python
How to Get If a Key Is Pressed Pygame
What's the Simplest Way of Detecting Keyboard Input in a Script from the Terminal
Get Ip Address of Visitors Using Flask for Python
The 'Is' Operator Behaves Unexpectedly with Non-Cached Integers
Change One Value Based on Another Value in Pandas
Stacked Bar Chart with Centered Labels
Python App Does Not Print Anything When Running Detached in Docker