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.
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.
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
get the variable name passed to a function in C
You can't print the address of a variable using function like you've shown. The reason is that a
is local variable, which has a different identity than the variable whose value you passed to printAddress
.
You also can't get the name, because the name doesn't exist in that context.
However, you could use a macro:
#define printAddress(x) printf("Address of variable %s is %p\n", #x, &x)
Note that #
here is the stringification operator of the preprocessor; it turns a token into a C string literal (effectively just adding quotes around the token).
Full example:
#include <stdio.h>
#define printAddress(x) printf("Address of variable %s is %p\n", #x, &x)
int main(void)
{
int foo;
printAddress(foo);
return 0;
}
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.
Get Original Variable Name From Function in Ruby
Implementation of the idea from the comment, but I reiterate that it's extremely fragile and a really bad idea all around.
def orig_var_name(var)
loc = caller_locations.first
line = File.read(loc.path).lines[loc.lineno - 1]
line[/#{__method__}\(\s*(\w+)\s*\)/, 1]
rescue Errno::ENOENT
raise "Not usable from REPL"
end
foo = 1
puts orig_var_name(foo)
# => foo
Related Topics
Parse Date Without Timezone JavaScript
Cancel a Vanilla Ecmascript 6 Promise Chain
How to Use Multiple Refs for an Array of Elements with Hooks
Making a JavaScript String SQL Friendly
How to Create a Custom Scrollbar on a Div (Facebook Style)
How to Find a Parent with a Known Class in Jquery
How to Pass Variable Value Between Different HTML Pages in JavaScript
Access Event to Call Preventdefault from Custom Function Originating from Onclick Attribute of Tag
Get Mouse Wheel Events in Jquery
Are Variable Operators Possible
Recursively Looping Through an Object to Build a Property List
How Might I Find the Largest Number Contained in a JavaScript Array
Axios Posting Params Not Read by $_Post
JavaScript Onclick Event Over Flash Object