Loading video player…

Asking for Forgiveness

00:00 So now we come to EAFP, Easier to Ask Forgiveness Than Permission. What exactly is that? It’s a common phrase. If you are now or have ever been a teenager, I’m sure it’s familiar to you, at least in concept.

00:14 Basically, you’re going to go out and do what you want, throw caution to the wind, and if you make mistakes, that is, if you encounter an error, you’ll handle it after it happens.

00:25 Another way to put that would be if you get caught sneaking out after curfew, you better hope you have forgiving parents. So how does this apply to code? With EAFP we’ll be handling errors when they occur.

00:38 You’ll do this using the Python try ... except syntax, which allows you to catch an error after it happens. To apply EAFP well, you’ll have to identify the actions that you may take that may cause errors.

00:52 Now let’s go back to the REPL for another example.

00:56 First things first, start up the Python REPL. Once again, create a dictionary. For consistency’s sake, I’m going to use the same key-value pair as in the previous video.

01:07 And what can I say? I like my name. It doesn’t really matter much as the main purpose of this dictionary is to produce an error. Now that you’ve got your dictionary, you can imagine that some time passes, maybe you get a call from a friend and when you come back you don’t quite recall exactly what was in the dictionary and you try to access a key email that doesn’t exist.

01:30 And as you should expect by now, this causes an error because the key email is not present in the dictionary, Python raises a KeyError.

01:39 So how can you take care of this or handle this error using EAFP? You’re going to use try ... except.

01:47 So first in the try block, you contain any of the code that could potentially raise an error. In this case, trying to assign the value of the dictionary key email to a variable.

01:59 In case a KeyError is raised, the except will be triggered. And if the except is triggered, the print() function will print out a message letting you know that the error was raised, but was also handled.

02:12 Let’s see if this works. There you go. Because a KeyError was raised, our error was handled by our except block. And in that way, you just used EAFP to handle an error instead of avoiding it.

02:27 So I know I’ve mentioned that the try ... except Python syntax as well as EAFP as a style is not something that’s available in all programming languages.

02:36 So this might lead you to wonder is EAFP itself more Python? Well, as an answer, I’ll start with a quote from Guido Van Rossum, the creator of the Python programming language.

02:47 “I disagree with the position that EAFP is better than LBYL or generally recommended by Python.

02:55 The way I see it, it depends. Some situations call for EAFP, while others merit the use of LBYL. There’s no such thing as one coding style to rule them all.” So where does that leave us?

03:08 Well, in the words of Leonardo DiCaprio, we have to go deeper. Over the next few lessons, we’ll see some cases where EAFP dominates, some things that you really need to watch out for when using EAFP, and then finally, we’ll have a comparison with guidelines to help you make the right call for a given situation.

03:26 Let’s go.

Become a Member to join the conversation.