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.
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.
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.
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;
}
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
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
Related Topics
How to Execute a Stored Procedure in PHP Using SQLsrv and "" Style Parameters
Why Is Calling a Function (Such as Strlen, Count etc) on a Referenced Value So Slow
JavaScript Function Post and Call PHP Script
Interface Implementation: Declaration Must Be Compatible
$_Post Is Empty After Form Submit
How to Obtain a Nested HTML List from Object's Array Recordset
How to Send Emails with Arabic Content via PHP's Mail Function
Setting a Connect Timeout with Pdo
Max_File_Size in PHP - What's the Point
Do I Really Need to Do MySQL_Close()
Is There a JavaScript Way to Do File_Get_Contents()
Pdo: Invalid Parameter Number: Mixed Named and Positional Parameters