Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8421323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:07:16+00:00 2026-06-10T03:07:16+00:00

I have a dict of strings, and those strings correspond to function names. I’m

  • 0

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.

vars() working:

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

vars() not working (within a function):

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)
<ipython-input-22-15adf33a5fea> in <module>()
----> 1 run_it()

<ipython-input-21-5663ac30227d> in run_it()
      1 def run_it():
      2     name = 'brian'
----> 3     x = vars()[name]
      4     x()
      5 

KeyError: 'brian'

And finally, here’s vars() (which does include the key ‘brian’):

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__': <module '__builtin__' (built-in)>,
 '__builtins__': <module '__builtin__' (built-in)>,
 '__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': <module 'IPython.core.shadowns' from '/Library/Frameworks/Python.framework/Versions    
/2.7/lib/python2.7/site-packages/IPython/core/shadowns.pyc'>,
 'brian': <function __main__.brian>,
 'exit': <IPython.core.autocall.ExitAutocall at 0x1598cb0>,
 'get_ipython': <bound method TerminalInteractiveShell.get_ipython of     
<IPython.frontend.terminal.interactiveshell.TerminalInteractiveShell object at 0x1589f70>>,
 'help': Type help() for interactive help, or help(object) for help about object.,
 'quit': <IPython.core.autocall.ExitAutocall at 0x1598cb0>,
 'run_it': <function __main__.run_it>}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:07:17+00:00Added an answer on June 10, 2026 at 3:07 am

    Inside the function, vars() is the same as locals(). So no function outside the function scope is allowed.

    You may want to use globals() (the behavior of vars() outside functions)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dict of configuration variables that looks something like this: self.config =
I have a plist that looks like this: <dict> <key>Aaron</key> <dict> <key>number</key> <string>1234</string> <key>country</key>
My issue is as follows. We have dict that has all keys and values
I have a csv dump that I'm trying to import to run analysis on
I have an NSArray of strings that I would like to animate with a
I have a function with many input parameters, and I need a function that
I have a dictionary as follows: IDictionary<string, string> dict; How to create an enumerator
Have dict like: mydict= {'a':[],'b':[],'c':[],'d':[]} list like: log = [['a',917],['b', 312],['c',303],['d',212],['a',215],['b',212].['c',213],['d',202]] How do i
I have a dict object. I dumped the data using this: for alldata in
I have a dict which contains some lists and some dicts, as illustrated below.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.