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

  • SEARCH
  • Home
  • 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 7818893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:43:54+00:00 2026-06-02T06:43:54+00:00

I want to know what is the best way to map the string to

  • 0

I want to know what is the best way to map the string to a function.
so far I know I can use:

  1. globals()[func_string]
  2. sys.modules[__name__]
  3. funcs_dictionary[“func_string” : func]

Here is the code:

    >>> def foo(arg):
    ...     print "I'm foo: %s" % arg
    ... 
    >>> def bar(arg):
    ...     print "I'm bar: %s" % arg
    ... 
    >>> 
    >>> foo
    <function foo at 0xb742c7d4>
    >>> bar
    <function bar at 0xb742c80c>
    >>> 

globals()[func_string]:

    >>> def exec_funcs_globals(funcs_string):
    ...     for func_string in funcs_string:
    ...         func = globals()[func_string]
    ...         func("from globals() %s" % func)
    ... 
    >>> exec_funcs_globals(["foo", "bar"])
    I'm foo: from globals() <function foo at 0xb742c7d4>
    I'm bar: from globals() <function bar at 0xb742c80c>
    >>> 

sys.modules[__name__]:

    >>> import sys
    >>> 
    >>> def exec_funcs_thismodule(funcs_string):
    ...     thismodule = sys.modules[__name__]
    ...     for func_string in funcs_string:
    ...         func = getattr(thismodule, func_string)
    ...         func("from thismodule %s" % func)
    ... 
    >>> exec_funcs_thismodule(["foo", "bar"])
    I'm foo: from thismodule <function foo at 0xb742c7d4>
    I'm bar: from thismodule <function bar at 0xb742c80c>
    >>> 

funcs_dictionary[“func_string” : func]:

    >>> funcs = {
    ...     "foo" : foo,
    ...     "bar" : bar
    ... }
    >>> 
    >>> def exec_funcs_dict(funcs_string):
    ...     for func_string in funcs_string:
    ...         func = funcs[func_string]
    ...         func("from thismodule %s" % func)
    ... 
    >>> exec_funcs_dict(["foo", "bar"])
    I'm foo: from thismodule <function foo at 0xb742c7d4>
    I'm bar: from thismodule <function bar at 0xb742c80c>

Originally I was worry about the sys.modules[__name__] will reload the modules and hurt the performance. But the code above seem to show that the function pointers are the same, so I guess I don’t have to worry about it?

What is the best use case for the option 1, 2, 3?

  • 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-02T06:43:56+00:00Added an answer on June 2, 2026 at 6:43 am

    Using globals() is the normal way to access (and store) variables you want to access globally.

    Otherwise, the usual choice is to implement a dispatch dictionary (like your option 3).

    I’ve not seen the sys.modules approach used anywhere.

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

Sidebar

Related Questions

I am developing an android app and I want to know the best way
I've been at this for a while and want to know the best way
i want to know which parsing method is best to use among Simple XML
I want to know what framework/Interface/API's Set is best to use to create multi-process
I want to know the best way to compare two objects and to find
i want to know the best way (faster and better) to structure the code
I want to know the best way of testing data access functionality. I know
I want to know the best way to mark up tabular data on a
I want to know the best way of transferring a cryptographic initialization vector (IV)
I want to know the best way of hosting a web application for different

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.