Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Mapping Conditions to Handler Functions

This lesson will show you how to use a dictionary to map conditions to handler functions. Here’s a code snippet from the lesson that shows how to create a dictionary mapping as well as how to call a function based on a condition:

Python
func_dict = {
    'cond_a': handle_a,
    'cond_b': handle_b,

}
func_dict.get(cond, handle_default)()

You’ll also learn that dictionaries can be a lot faster than using if statements because of the way they look up keys.

00:00 All right. So, let’s come back to our original plan here, which was emulating a switch/case statement. So, now that we know about these first-class functions, what we can do here is actually create a function dictionary that maps from the condition name—or, from the actual condition to the handler function.

00:21 Right? So, something like this, where we’re just mapping from the condition to the handler function. Then, I could look up that handler function just by passing the condition as a key into that dictionary, right?

00:41 And then I could just call the resulting function. And this would actually be—sort of—a switch/case statement, right? And we could shorten that further—I mean, you know, we don’t have to give a name to this.

00:57 Like, we could just do this, and this would already be kind of terse or, you know, there’s not a lot of overhead to this. But it has a big downside, and that is we’re going to get a KeyError

01:11 if the condition isn’t found. There’s no default at the moment, right? Previously with the if statement we had an else all the way at the bottom, and that would allow us to have a default branch that we can take and a default handler that we would call—and we can’t really do that right now.

01:31 So actually, one way around that is… And let me undo some of that.

01:39 So actually, one way around that would be to use the .get() function on the dictionary. We could just call .get(), and then that would either return the value for that key, if it’s found, or it would return a default. In this case, we would just pass in a handle_default function that we’d have to define, and then call that. So, this would actually solve the problem with the KeyError, and we’d be able to have a default and have this dispatch based on this function dictionary here, which is already kind of neat. And you can imagine, like, this becomes really useful if you have many, many, many functions that you want to map to, or many conditions that you want to map to functions.

02:24 Because then it actually becomes a lot more efficient than an if / else statement, because an if / elseit needs to try all these conditions in sequence, but with a dictionary, it can actually take a shortcut and do a lookup based on the hash of that condition.

02:39 That can be a lot faster if you’re dealing with many of these mappings. I mean, if you’re only dealing with tens or potentially even hundreds of them, it won’t matter, so it would be a premature optimization, but for a really large number of functions and conditions, this could be extremely helpful.

02:56 So, this could be a really nice trick for you to actually speed up your Python code, to optimize it. So now, this might look kind of weird syntactically, and there’s actually one step we can go beyond this to clean this up.

03:12 I want to show you how to do that now.

Become a Member to join the conversation.