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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:51:16+00:00 2026-05-26T10:51:16+00:00

I have a situation where I have six possible situations which can relate to

  • 0

I have a situation where I have six possible situations which can relate to four different results. Instead of using an extended if/else statement, I was wondering if it would be more pythonic to use a dictionary to call the functions that I would call inside the if/else as a replacement for a “switch” statement, like one might use in C# or php.

My switch statement depends on two values which I’m using to build a tuple, which I’ll in turn use as the key to the dictionary that will function as my “switch”. I will be getting the values for the tuple from two other functions (database calls), which is why I have the example one() and zero() functions.

This is the code pattern I’m thinking of using which I stumbled on with playing around in the python shell:

def one():
    #Simulated database value
    return 1

def zero():
    return 0

def run():
    #Shows the correct function ran
    print "RUN"
    return 1

def walk():
    print "WALK"
    return 1

def main():
    switch_dictionary = {}

    #These are the values that I will want to use to decide
    #which functions to use
    switch_dictionary[(0,0)] = run
    switch_dictionary[(1,1)] = walk

    #These are the tuples that I will build from the database
    zero_tuple = (zero(), zero())
    one_tuple = (one(), one())

    #These actually run the functions. In practice I will simply 
    #have the one tuple which is dependent on the database information
    #to run the function that I defined before
    switch_dictionary[zero_tuple]()
    switch_dictionary[one_tuple]()

I don’t have the actual code written or I would post it here, as I would like to know if this method is considered a python best practice. I’m still a python learner in university, and if this is a method that’s a bad habit, then I would like to kick it now before I get out into the real world.

Note, the result of executing the code above is as expected, simply “RUN” and “WALK”.

edit

For those of you who are interested, this is how the relevant code turned out. It’s being used on a google app engine application. You should find the code is considerably tidier than my rough example pattern. It works much better than my prior convoluted if/else tree.

def GetAssignedAgent(self):
    tPaypal = PaypalOrder() #Parent class for this function
    tAgents = []
    Switch = {}

    #These are the different methods for the actions to take
    Switch[(0,0)] = tPaypal.AssignNoAgent
    Switch[(0,1)] = tPaypal.UseBackupAgents
    Switch[(0,2)] = tPaypal.UseBackupAgents
    Switch[(1,0)] = tPaypal.UseFullAgents
    Switch[(1,1)] = tPaypal.UseFullAndBackupAgents
    Switch[(1,2)] = tPaypal.UseFullAndBackupAgents
    Switch[(2,0)] = tPaypal.UseFullAgents
    Switch[(2,1)] = tPaypal.UseFullAgents
    Switch[(2,2)] = tPaypal.UseFullAgents

    #I'm only interested in the number up to 2, which is why
    #I can consider the Switch dictionary to be all options available.
    #The "state" is the current status of the customer agent system
    tCurrentState = (tPaypal.GetNumberofAvailableAgents(), 
                     tPaypal.GetNumberofBackupAgents())

    tAgents = Switch[tCurrentState]()
  • 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-26T10:51:17+00:00Added an answer on May 26, 2026 at 10:51 am

    Consider this idiom instead:

    >>> def run():
    ...   print 'run'
    ... 
    >>> def walk():
    ...   print 'walk'
    ... 
    >>> def talk():
    ...    print 'talk'
    >>> switch={'run':run,'walk':walk,'talk':talk}
    >>> switch['run']()
    run
    

    I think it is a little more readable than the direction you are heading.

    edit

    And this works as well:

    >>> switch={0:run,1:walk} 
    >>> switch[0]()
    run
    >>> switch[max(0,1)]()
    walk
    

    You can even use this idiom for a switch / default type structure:

    >>> default_value=1
    >>> try:
    ...    switch[49]()
    ... except KeyError:
    ...    switch[default_value]()
    

    Or (the less readable, more terse):

    >>> switch[switch.get(49,default_value)]()
    walk
    

    edit 2

    Same idiom, extended to your comment:

    >>> def get_t1():
    ...    return 0
    ... 
    >>> def get_t2():
    ...    return 1
    ... 
    >>> switch={(get_t1(),get_t2()):run}
    >>> switch
    {(0, 1): <function run at 0x100492d70>}
    

    Readability matters

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

Sidebar

Related Questions

I have situation in which I read a record from a database. And if
I have situation where a user can manipulate a large set of data (presented
We have situation where say we have four engineers that are working on software
Oftentimes, when using git, I find myself in this situation: I have changes to
I have situation where I have to call method of interface using reflection, like
I have situation in which i'm compelled to retrieve 30,000 records each to 2
I have situation where i have column in mysql table from which i populate
Situation: I have a calculator that I'm developing using these formulas: // --- Math
I can't make git push origin B. I have situation something like this _____________________________________
Situation: Have a UITextField instance which is a subview of a UIScrollView. Need: Screen

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.