Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy can't I use vars() when I try to call it within a function?
    primarykey
    data
    text
    <p>I have a dict of strings, and those strings correspond to function names. I'm trying to use vars() to assign those strings to variables that I can then use for function calls. I'm able to get vars() to work outside of a function, but when I try to call it within a function, I get a KeyError. The code below obviously isn't the actual code I am having trouble with, but the problem is the same: I can find the key in the vars() dict, but I can't call it from a function. </p> <p>vars() working:</p> <pre><code>In [1]: def brian(): ...: print "this is the brian function" ...: In [2]: name = 'brian' In [3]: x = vars()[name] In [4]: x() this is the brian function </code></pre> <p>vars() not working (within a function):</p> <pre><code>In [20]: def brian(): ....: print "this is the brian function" ....: In [21]: def run_it(): ....: name = 'brian' ....: x = vars()[name] ....: x() ....: In [22]: run_it() --------------------------------------------------------------------------- KeyError Traceback (most recent call last) &lt;ipython-input-22-15adf33a5fea&gt; in &lt;module&gt;() ----&gt; 1 run_it() &lt;ipython-input-21-5663ac30227d&gt; in run_it() 1 def run_it(): 2 name = 'brian' ----&gt; 3 x = vars()[name] 4 x() 5 KeyError: 'brian' </code></pre> <p>And finally, here's vars() (which <em>does</em> include the key 'brian'):</p> <pre><code>In [4]: vars() Out[4]: {'In': ['', u'def brian():\n print "this is the brian function"\n ', u"def run_it():\n name = 'brian'\n x = vars()[name]\n x()\n ", u'run_it()', u'vars()'], 'Out': {}, '_': '', '__': '', '___': '', '__builtin__': &lt;module '__builtin__' (built-in)&gt;, '__builtins__': &lt;module '__builtin__' (built-in)&gt;, '__doc__': 'Automatically created module for IPython interactive environment', '__name__': '__main__', '_dh': [u'/Users/brian'], '_i': u'run_it()', '_i1': u'def brian():\n print "this is the brian function"\n ', '_i2': u"def run_it():\n name = 'brian'\n x = vars()[name]\n x()\n ", '_i3': u'run_it()', '_i4': u'vars()', '_ih': ['', u'def brian():\n print "this is the brian function"\n ', u"def run_it():\n name = 'brian'\n x = vars()[name]\n x()\n ", u'run_it()', u'vars()'], '_ii': u"def run_it():\n name = 'brian'\n x = vars()[name]\n x()\n ", '_iii': u'def brian():\n print "this is the brian function"\n ', '_oh': {}, '_sh': &lt;module 'IPython.core.shadowns' from '/Library/Frameworks/Python.framework/Versions /2.7/lib/python2.7/site-packages/IPython/core/shadowns.pyc'&gt;, 'brian': &lt;function __main__.brian&gt;, 'exit': &lt;IPython.core.autocall.ExitAutocall at 0x1598cb0&gt;, 'get_ipython': &lt;bound method TerminalInteractiveShell.get_ipython of &lt;IPython.frontend.terminal.interactiveshell.TerminalInteractiveShell object at 0x1589f70&gt;&gt;, 'help': Type help() for interactive help, or help(object) for help about object., 'quit': &lt;IPython.core.autocall.ExitAutocall at 0x1598cb0&gt;, 'run_it': &lt;function __main__.run_it&gt;} </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload