Dealing with errors and exceptional situations is a common requirement in programming. You can either prevent errors before they happen or handle errors after they’ve happened. In general, you’ll have two coding styles matching these strategies: look before you leap (LBYL), and easier to ask forgiveness than permission (EAFP), respectively. In this tutorial, you’ll dive into the questions and considerations surrounding LBYL vs EAFP in Python.
By learning about Python’s LBYL and EAFP coding styles, you’ll be able to decide which strategy and coding style to use when you’re dealing with errors in your code.
In this tutorial, you’ll learn how to:
- Use the LBYL and EAFP styles in your Python code
- Understand the pros and cons of LBYL vs EAFP
- Decide when to use either LBYL or EAFP
To get the most out of this tutorial, you should be familiar with how conditional statements and try
… except
statements work. These two statements are the building blocks for implementing the LBYL and EAFP coding styles in Python.
Free Bonus:5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.
Errors and Exceptional Situations: Preventing or Handling Them?
Dealing with errors and exceptional situations is a fundamental part of computer programming. Errors and exceptions are everywhere, and you need to learn how to manage them if you want robust and reliable code.
You can follow at least two general strategies when it comes to dealing with errors and exceptions:
- Prevent errors or exceptional situations from happening
- Handle errors or exceptional situations after they happen
Historically, preventing errors before they happen has been the most common strategy or approach in programming. This approach typically relies on conditional statements, also known as if
statements in many programming languages.
Handling errors and exceptions after they’ve happened came onto the scene when programming languages started to provide exception-handling mechanisms, such as try
… catch
statements in Java and C++, and try
… except
statements in Python. However, in Java and C++, handling exceptions can be a costly operation, so these languages tend to prevent errors rather than handling them.
Note: One optimization coming in Python 3.11 is zero-cost exceptions. This implies that the cost of try
statements will be almost eliminated when no exception is raised.
Other programming languages like C and Go don’t even have exception-handling mechanisms. So, for example, Go programmers are used to preventing errors using conditional statements, like in the following example:
funcSomeFunc(argint)error{result,err:=DoSomething(arg)iferr!=nil{// Handle the error here...log.Print(err)returnerr}returnnil}
This hypothetical Go function calls DoSomething()
and stores its return values in result
and err
. The err
variable will hold any error that occurs during the function execution. If no error occurs, then err
will contain nil
, which is the null value in Go.
Then the if
statement checks if the error is different from nil
, in which case, the function proceeds to handle the error. This pattern is pretty common, and you’ll see it repeatedly in most Go programs.
Python’s exception-handling mechanisms are pretty efficient when no exception is raised. Therefore, in Python, it’s common and sometimes encouraged to deal with errors and exceptional situations using the language’s exception-handling syntax. This practice often surprises people who come from other programming languages.
What this means for you is that Python is flexible and efficient enough that you can select the right strategy to deal with errors and exceptional situations in your code. You can either prevent errors with conditional statements or handle errors with try
… except
statements.
Pythonistas typically use the following terminology to identify these two strategies for dealing with errors and exceptional situations:
Strategy | Terminology |
---|---|
Preventing errors from occurring | Look before you leap (LBYL) |
Handling errors after they occur | Easier to ask forgiveness than permission (EAFP) |
In the following sections, you’ll learn about these two strategies, also known as coding styles in Python and other programming languages.
Programming languages with costly exception-handling mechanisms tend to rely on checking for possible errors before they occur. These languages generally favor the LBYL style. Python, in contrast, is more likely to rely on its exception-handling mechanism when dealing with errors and exceptional situations.
With this brief introduction on strategies to deal with errors and exceptions, you’re ready to dive deeper into Python’s LBYL and EAFP coding styles and explore how to use them in your code.
The “Look Before You Leap” (LBYL) Style
Read the full article at https://realpython.com/python-lbyl-vs-eafp/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]