1

note on closing: this question has been suggested as a duplicate but the two questions actually ask different questions: find the name of a variable versus access a value by name. May Heaven bless readers who carefully review questions before pushing the dupe button.

Python - find the name of a variable that was passed to the function


Suppose I have a function to print a variable's contents:

def printVariable(varname, val):
    print "%s: %s" % (varname, val)

printVariable('x', x)

How can I modify this to look at the calling stack and extract the variable's value?

def printVarible(varname):
    val = WHAT MAGIC GOES HERE?
    print "%s: %s" % (varname, val)

printVariable('x')

Variable resolution should be at the scope of the caller... if it's in a function and there's a local variable, it should be preferred over a global variable.

Mark Harrison
  • 297,451
  • 125
  • 333
  • 465

3 Answers3

2

The inspect module will allow you to pull values out of the calling stack. This function will take a list of variables and print their values

import inspect
def printVaribles(*args):
    for arg in args:
        print "%12s:%s"%(arg, inspect.stack()[1][0].f_locals[arg])

printVariables('x','y','z')
Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
  • 1
    If this question was [different](http://stackoverflow.com/questions/23327633/python-access-a-variables-value-in-the-calling-stack-by-name?noredirect=1#comment35721162_23327633), how is your answer so similar to [this one](http://stackoverflow.com/a/8856111/2235132) in the referenced duplicate? – devnull Apr 28 '14 at 16:10
  • BTW, congratulations for the honorable mention in a [meta post](http://meta.stackoverflow.com/questions/252506/question-quality-is-dropping-on-stack-overflow). – devnull Apr 28 '14 at 16:11
  • The answer is similar because the other poster was trying (and failing) to ask this question. But you're voting for a duplicate answer and not a duplicate question. – Mark Harrison Apr 28 '14 at 17:17
  • 1
    Where am I in the meta comment? I couldn't find it!! BTW did you know I'm the person responsible for setting up meta? I was having lunch with Jeff and telling him that no amount of hectoring would stop people from discussing the site on the site and that his only choice was to make another place for people to discuss the site. – Mark Harrison Apr 28 '14 at 17:18
0

You want to evaluate the variable, so just:

val = eval('x')

If you want to know what is defined, you can take a look at the dictionaries provided by locals() and globals().

Davidmh
  • 3,797
  • 18
  • 35
0

It's a little weird, but you might could use **kwargs to achieve the desired effect.

def foo(**kwargs):
    return kwargs

In a use-case it'd be something like:

this = 13
foo(this=this) # returns {'this': 13}

If you use locals() you'll get whatever the name of the argument is in the definition of the function. For example...

def bar(your_arg):
    return locals()

The above will always return your_arg as the key of the dict, regardless of its name.