0

since there is no switch/case in python, i am trying to implement the same in my sample program with the help of the following link: why python doesnt have switch-case

below is my code:

#switch case to get the type of action to take
def action_type(action_id, dir_path):
    switcher = {
        1: func1(dir_path),
        2: func2(dir_path),
    }
    action_func = switcher.get(action_id, "Do Nothing")
    print "action_func, ", action_func
    sys.exit(0)
    return action_func()

now it always goes to func1 irrespective of the argument i.e. action_id passed

coolstoner
  • 719
  • 2
  • 9
  • 20
  • Why is `sys.exit(0)` the default in your `get` method? – Moses Koledoye Sep 09 '16 at 17:19
  • 1
    The code in your question defines a function `action_type` and inside that defines a function `number_to_action` which is never called, so the code is "print action id and then quit". If that's not your whole code, or it's indented wrongly in the question, you need to post more of it or edit it. – TessellatingHeckler Sep 09 '16 at 17:19
  • 1
    Are you expecting those `fun1`, `fun2`, and `sys.exit` calls to only happen conditionally? – user2357112 Sep 09 '16 at 17:19
  • well the number_to_action(action_id) was added as initially action_type always called func1 even if i supply argument 2 for func2 – coolstoner Sep 09 '16 at 17:21
  • @coolstoner switch/case implementation is not that complicated: http://stackoverflow.com/a/60211/2104879 – mertyildiran Sep 09 '16 at 17:23
  • Possible duplicate of [Replacements for switch statement in Python?](http://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python) – mertyildiran Sep 09 '16 at 17:24

2 Answers2

2

When you call switcher.get(action_id, sys.exit(0)), the 2nd argument is evalulated before the result can be passed in to .get(), which results in the program terminating immediately.

Wooble
  • 87,717
  • 12
  • 108
  • 131
1

Your code is broken in two ways, the first way is:

def action_type(action_id, dir_path):
    print "action id is, ", action_id

    def whatever():
        # all code here never runs

    # action_type() exits here

this inner function whatever() is defined, you never call it so it never runs, and then it stops existing when action_type exits, so nothing else can ever get to it.

The second problem is

switcher = {
   1: fun1(dir_path),
   2: func2(dir_path),
}

This calls the functions (both of them) at the time Python reads and initializes the dictionary (when it's parsing your code), and you don't want them both called, and you don't want them called until you ask for it. e.g.

test = { 
    1: len("hello")
}

# this immediately replaces `len("hello")` with the length of the string (5)

and the same with switcher.get(action_id, sys.exit(0)) - this calls sys.exit immediately.

To use 'first class functions' in Python - to pass them around without calling them - you need to use their name without parens. So...

def action_type(action_id, dir_path):
    print "action id is, ",action_id

    switcher = {
        1: fun1,
        2: func2,
    }

    action_func = switcher.get(action_id, sys.exit)

    print "action_func, ", action_func
    action_func(dir_path)


action_type(1, r"c:\wherever")

Pass the functions around without calling them. Then call one at the end.

You will still have to rethink so you don't call sys.exit with a dir_path.

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
  • yes, am almost there...yes got the string issue and had modified but i got another error now `def action_type(action_id, dir_path): switcher = { 1: 'files_to_lower_case', 2: 'files_to_lower_case_recursively' } print "switcher is , ",switcher action_func = switcher.get(action_id, "do nothing") print "action_func, ", action_func sys.exit(0) return action_func()` always returns "do nothing" – coolstoner Sep 09 '16 at 17:37
  • 1
    @coolstoner I'mma use my psychic debugging powers and say: 1) you're using Python 2.7, 2) you're prompting for the number for which function to run with `raw_input()` and 3) you're getting a string from the user, your dictionary keys are integers and you aren't doing any type conversion to match them up. How close am I? If so, change your switcher to `"1"` and `"2"` – TessellatingHeckler Sep 09 '16 at 17:40
  • bingo...i think u have nailed it....let me try and revert soon....i remember printing the action_id a little earlier and receiving '1' in my console.... should be the type conversion issue here as i get KeyError... – coolstoner Sep 09 '16 at 17:44
  • can you tell me why this doesnt work `action_func = switcher.get(int(action_id),sys.exit(0))` and this works `action_func = switcher[int(action_id)]` i just cant figure out...the first line always exits my code while the second one works – coolstoner Sep 09 '16 at 17:50