How to get the NAME of the passed parameter ?
Eg. in the example below how to get printed , inside my_func() , value = "test_var"

def my_func(aaa):
[INDENT]print aaa     # <---- this will print the VALUE of passed parameter 'aaa' [/INDENT] 
[INDENT]# ..... but how to print the NAME of the 'aaa' above as called by a parent function main() - i.e.

how to make it print 'test_var'[/INDENT]

def main():
[INDENT]test_var = 123[/INDENT]
[INDENT]my_func(test_var)[/INDENT]

Note that I do not want to get 'aaa' printed but 'test_var'

Recommended Answers

All 7 Replies

Local variables are kept in the function's local dictionary which you can access with vars(). To access one function's local dictionary in another function, you have to pass it along:

def my_func(aaa, main_var):
    mvar = [key for key, val in main_var.items() if val==aaa][0]
    print aaa, main_var, mvar  # 123 {'test_var': 123} test_var

def main():
    test_var = 123
    my_func(test_var, vars())

main()

thanks for a reply but can you re-write the mvar = ... line in more 'beginners' friendly format ? :
mvar = [key for key, val in main_var.items() if val==aaa][0]

i.e. few separate lines so that I would understand what ot does ?
Thanks

I hope you understand this better:

def main():
    a = 1
    b = 2
    c = 3
    # local variable dictionary
    main_var = vars()
    print main_var  # {'a': 1, 'c': 3, 'b': 2}

    # get the key (variable name) of a certain value, for instance 3
    mvar = [key for key, val in main_var.items() if val==3][0]
    print mvar  # c

    # in somewhat longer form ...
    mvar_list = []
    # extract the key:value pairs from the dictionary
    for key, val in main_var.items():
        # if value is 3 save the key in a list
        if val == 3:
            mvar_list.append(key)
    print mvar_list     # ['c']
    # pick the first item on the list
    print mvar_list[0]  # c

main()

Great thanks for the time you spent on it. Most appreciated. BTW: where can I find explanation of what this is (from your code above):

... = [key .... ]

i.e. [ followed by a reserved word

the code within [] is called list comprehension and it creates a list

...

q = [process(x) for x in somelist if condition(x)]

is a shorthand notation for ...
q = []
for x in somelist:
    if condition(x):
        q.append(process(x))

...

One more thing :
How to catch (inside my_func()) a situation of receiving a literal value instead of a variable name:
my_func(test_var, vars()) --- > my_func(123, vars())

thus avoiding this error when making a call to main():
< IndexError: list index out of range >

Note: I realize that my_func() is not able to recover the 'key' for literal data passed, but as long as it does not cause a run time error then I would be fine)

Nice observation! Trap the error with try/except like this:

def my_func(aaa, main_var=None):
    try:
        mvar = [key for key, val in main_var.items() if val==aaa][0]
        print aaa, main_var, mvar
    except:
        print aaa
    # other code follows
    pass

def main():
    test_var = 123
    my_func(test_var, vars())
    my_func(123, vars())
    my_func(456, vars())
    my_func(123)

main()

"""
my output -->
123 {'test_var': 123} test_var
123 {'test_var': 123} test_var
456
123
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.