![]() |
VOOZH | about |
We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.
Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.
Follow TNS on your favorite social media networks.
Become a TNS follower on LinkedIn.
Check out the latest featured and trending stories while you wait for your first TNS newsletter.
Error handling is a very important concept in programming. Without error handling, your applications wouldn’t be able to effectively handle abnormal input or conditions and would, most likely, fail to run or remain running. As a Python programmer, you need to ensure your applications are capable of countering errors that may have been brought about by users or your own code.
This can be tricky but fortunately, Python has a statement that makes it a bit easier. That statement is with and it serves to not only help your programs deal with errors but also to make your code far cleaner and easier to read (which is important within the realm of Python) and replaces the more convoluted try/finally statement.
Let me show you a simple application that uses the try/finally statement and then I’ll demonstrate the same application using with. This application will simply create a new file and write the text “Hello New Stack” to it. We’ll use the open(), write(), and close() functions to achieve this task (each of which is available in the standard library (so you don’t have to import anything first).
The try/finally version of this code looks like this:
A brief explanation:
Create that file using your editor of choice (naming it tryfinally.py) and run it with:
python3 tryfinally.py
You shouldn’t see any output, but if you view the contents of test_file, it should read:
Hello New Stack
Okay, let’s do the same thing, only this time we’ll use the with statement. We can now do this with only two lines that look like this:
An explanation:
Run this program and you’ll see the same results as you did in the app that used the try/finally statement. The difference is, that we were able to more cleanly and efficiently use error-handling. Although there is nothing wrong with using try/finally, the goal with Python is to write cleaner code that is more easily readable. By using fewer lines of code, you are less likely to introduce errors.
The true beauty of with is that it takes care of actions that would otherwise require more code to deal with (such as the opening and closing of a file).
Of course, with doesn’t just work with open(). You can even create objects that support with. Let’s create an object for file writing that uses with. We’ll do the same thing we did above (write Hello World to test_file), only this time we’ll do so by creating an object that will employ with.
There are a few things of note. We’re going to employ the following:
Our code looks like this:
The line-by-line looks like this:
When you run the program, you’ll get the same output as you did before (Hello New Stack written to test_file).
With is a very powerful and handy tool for Python error handling. By replacing the try/finally method with a more concise block of code, you not only ensure your programs reliably close resources after they are used, but you also write cleaner code that introduces fewer errors.