1
\$\begingroup\$

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".

\$\endgroup\$

closed as off-topic by Stephen Rauch, Billal Begueradj, Ludisposed, t3chb0t, Toby Speight Jun 4 '18 at 9:29

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Stephen Rauch, Billal Begueradj, Ludisposed, t3chb0t, Toby Speight
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ best practice questions are outside of the scope of code review \$\endgroup\$ – Winston Ewert Oct 22 '11 at 13:27
  • \$\begingroup\$ Please move this to Stack Overflow, where it belongs. \$\endgroup\$ – S.Lott Oct 27 '11 at 1:15
  • \$\begingroup\$ The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles. \$\endgroup\$ – Toby Speight Jun 4 '18 at 9:29
3
\$\begingroup\$

In short, a dictionary of functions IS the Python equivalent of the switch statement.

However, your implementation is awful. Start by dropping the "switch" in your variable names. It is not a switch, it is a dictionary. And there is no point in using "dictionary" in the name either. Make a real name like "act_on_msg" but even that is weak in my opinion.

And don't even explain that you are using a dictionary as a switch in your comments. At most, mention that actOnMsg[combomsg]() runs the action method just to help those who are not used to seeing parentheses in that context.

\$\endgroup\$
  • \$\begingroup\$ It was just pattern code, not my real code, hence the odd names and comments but point taken. =) \$\endgroup\$ – Ken Oct 22 '11 at 7:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.