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 591729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:39:54+00:00 2026-05-13T15:39:54+00:00

This is for use in a JSON API. I don’t want to have: if

  • 0

This is for use in a JSON API.
I don’t want to have:

if method_str == 'method_1':
    method_1()

if method_str == 'method_2':
    method_2()

For obvious reasons this is not optimal. How would I use map strings to methods like this in a reusable way (also note that I need to pass in arguments to the called functions).

Here is an example:

INCOMING JSON:

{
    'method': 'say_something',
    'args': [
        135487,
        'a_465cc1'
    ]
    'kwargs': {
        'message': 'Hello World',
        'volume': 'Loud'
    }
}

# JSON would be turned into Python with Python's built in json module.

Resulting call:

# Either this
say_something(135487, 'a_465cc1', message='Hello World', volume='Loud')

# Or this (this is more preferable of course)
say_something(*args, **kwargs)
  • 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-05-13T15:39:54+00:00Added an answer on May 13, 2026 at 3:39 pm

    For methods of instances, use getattr

    >>> class MyClass(object):
    ...  def sayhello(self):
    ...   print "Hello World!"
    ... 
    >>> m=MyClass()
    >>> getattr(m,"sayhello")()
    Hello World!
    >>> 
    

    For functions you can look in the global dict

    >>> def sayhello():
    ...  print "Hello World!"
    ... 
    >>> globals().get("sayhello")()
    Hello World!
    

    In this case, since there is no function called prove_riemann_hypothesis the default function (sayhello) is used

    >>> globals().get("prove_riemann_hypothesis", sayhello)()
    Hello World!
    

    The problem with this approach is that you are sharing the namespace with whatever else is in there. You might want to guard against the json calling methods it is not supposed to. A good way to do this is to decorate your functions like this

    >>> json_functions={}
    >>> def make_available_to_json(f):
    ...  json_functions[f.__name__]=f
    ...  return f
    ...
    >>> @make_available_to_json
    ... def sayhello():
    ...  print "Hello World!"
    ...
    >>> json_functions.get("sayhello")()
    Hello World!
    >>> json_functions["sayhello"]()
    Hello World!
    >>> json_functions.get("prove_riemann_hypothesis", sayhello)()
    Hello World!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I've looked at this use of the freebase API and I was really
I use the Action<object>.BeginInvoke() method, does this use the thread pool or not? I
I want to use Goolge AJAX Feed API in my C# console application to
I have a JSON feed (which I don't directly control, I have to ask
I use this code to gathering the information from Wiki: http://en.wikipedia.org/w/api.php?action=query&rvprop=content&prop=revisions&format=json&titles=apple And I can
I am working on a App that makes use of a JSON API and
I want to use a temp directory that will be unique to this build.
I'm looking at some GXT code for GWT and I ran across this use
I use this tool called Lazy C++ which breaks a single C++ .lzz file
I use this line in my .htaccess file to automatically add a trailing slash

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.