Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Dictionaries
Lecture: Dictionaries as switch statements

Login or purchase this course to watch this video and the rest of the course contents.
0:01 The Python language has no switch statement. We have "ifs" and we have "for...in" loops and we have "while" loops and that's about it. No switch.
0:10 But, we'll see that with a dictionary, we can actually simulate a switch statement, so let's look at that in PyCharm.
0:17 Let's see what program we have here. So it starts out by asking which direction presumably do you want to move,
0:22 we have this little character, his name is Chippy, so Chippy is going to move in a direction, and what we need to do is we are going to ask,
0:30 provide some options: North, South, East, West, South-West, that kind of thing, and then we are going to parse this into a moves enumeration,
0:38 you can see we have these various options here, so we are going to focus on this parse method
0:43 and then later we'll have a quick glance of this move method as well. So down here, we have the standard parse
0:50 and what we are going to do is we are going to go through all the possible cases, if you have a "w", we would like you to move West,
0:56 if you have an "s", we'd like you move South or we'll parse this to a "moves.south" and so on.
1:02 So we'll see that this whole segment here can be replaced. How does that work? You can replace all these tests for if with basically a key access
1:11 so let's go over here and we'll call this like parse_dict or something like that, and we can just start printing the values
1:17 so for "w" we are going to have this one, for "s" we are going to have this one, and so on, let me zoom ahead to build this whole dictionary.
1:30 So here we have our dictionary built, so how do we use this to actually replace that "if" statement, we'll just say "return parse_dict.get(text)",
1:41 so we are actually doing two things here, one we - are looking up the values for each "if" statement
1:45 by going into the keys here and because we are using "get" rather than indexing directly into it, we are actually providing the default return value
1:54 that was at the very end of that "if" statement, cool, right? So let's run it and make sure that this still works,
2:01 so we want to move South East, it says "You chose: Moves.SouthEast", we print it out to just see what we parse to make sure
2:09 that that's working, and then Chippy moves South East, yupi, what if he goes North? Oh, Chippy is moving North,
2:14 so really cool and clever way to build that switch statement for certain circumstances, when you have this really large set of possibilities.
2:22 Theoretically, it can even be faster; so what we have done here is we basically have returned the value given a particular case,
2:30 but in switch statements, you can also call functions, right? It's not just a matter of saying "we are going to pick a value"
2:36 but you can actually say "we are going to run this block of code if this case matches or that block of code if that case matches."
2:43 So let's move down to our character here and make a change there, so, right now it just prints out the same thing, no matter what direction you move,
2:50 but what if I wanted to do something like this, "Direction == Moves.North", let's print, "character name moves North with a special hesitation",
3:06 "else", otherwise we'll just print they move in some direction. So let's test this. All right, so if we move South East, "Chippy moves South East",
3:17 if you move North though, "Chippy moves North with a special hesitation", so could we take this behavior and actually
3:24 make this into a dictionary switch style statement? Answer is of course we can, so what does the dictionary look like?
3:31 So we are going to need again to define the keys but this time the key is going to actually be the value for direction,
3:39 and I added a little type annotation here to say this is moves, so this would be like a Moves.North and then we are going to put something here,
3:47 let me just put None for a minute, so we have one for North and one for South. So what we need to put here is executable code,
3:54 we could put a function name that we write somewhere else, or we could just write a lambda, something like this,
4:07 "Character moves North with the special hesitation", and so on. Now let's just put these two for a minute, just to see how this idea is going to work,
4:16 so here we have our action_dict and let's have a default_action that we will use if for some reason we get a move we don't expect.
4:25 So here we've got default action "so and so moves quickly to such and such direction." So then, what we can do is I can say the action is equal,
4:33 I am using a lot of variables here, we could sort of inline this more possibly but trying to be really clear for you,
4:38 so "action_dict.get" and we are going to give it the direction, but we can also give it the default, action so that
4:46 if there is no direction method we get this default one and then we can just call "action()" Like so, OK, let's run this.
4:54 What directions do we have to move? We could say North, we should see "Chippy moves North with a special hesitation",
5:00 awesome, let's go South, "Chippy is going South for the winter!" Now, if we run this and we go South East which is not on our list,
5:09 "Chippy moves quickly to Moves.Southeast" Cool, right, so basically here is our switch statement, we probably could inline this a little bit better,
5:18 like for example this probably makes more sense inlining, it's, PEP 8 says we should define this as a "def" if we are going to write it that way,
5:26 so we can do this, that's a little on a long side, maybe we format it like so, of course,
5:31 we want to fill this out for all the reasonable actions that we'd like to handle, so this is nice for really short, small bits of executable code,
5:39 I wouldn't write too much code in these little these lambda expressions here but using this to select values
5:47 as if you had a bunch of "if" statements, this is really nice, so let's see this concept of dictionaries as switch statements in a graphic.
5:54 So here we have basically the code that you just looked at, the moves lookup, we are going to let these specified "w" for West,
6:01 "e" for East, "s" for South and so on, then we just say "dictionary.get", give it the text we want to switch on, conceptually,
6:08 and we'll either get one of the values we specified or we get None which is a pretty reasonable response for "Hey, I couldn't parse that value."
6:16 We saw that these values that go into the dictionary, like in this case Moves.West, it doesn't have to just be a value,
6:22 it can actually be an executable function as we saw on our character class.

Talk Python's Mastodon Michael Kennedy's Mastodon