the holy grail of Pythonic exception handling?

Hey there,

I'm surprised by it time and time again, but with programming, you truly *never* stop learning new things:

The other day PythonistaCafe member Greg posted a new discussion thread where he shared an example of the "contextlib.suppress()" function available in Python 3.4 (and above) that I thought was really cool.

Here's the gist of it—

You can use contextlib.suppress() to selectively ignore specific exceptions using a context manager and the "with" statement:
 

import contextlib

with contextlib.suppress(FileNotFoundError):
    os.remove('somefile.tmp')


This is equivalent to the following try/except clause:
 

try:
    os.remove('somefile.tmp')
except FileNotFoundError:
    pass


I think "suppress()" is a lovely little helper function—the resulting code is quite readable and clear.

Even a Python novice with just a tiny bit of experience with exceptions should be able to figure out what's going on.

And even someone who's never worked with exceptions in their code yet, they should be able to understand this code snippet because it reads (almost) like an English sentence. 

It's beautiful :-)

I've been working with Python for a while now, but somehow I managed to miss this little feature until I saw Greg's post (Thanks, Greg!)

So what are the lessons learned here? Two things:

Well, like I said, with programming you'll never truly "know it all." Expertise in our industry is a moving target, and there's will always be new things to learn and to integrate into your working life. And I think that's wonderful!

Is learning about this little "trick" going to change my life as a Pythonista? Not really. But I still very much appreciate the little endorphin spike I got from reading Greg's post.

And then, there's the immense value you get from being part of the Python community at large. Whether that's through something like this newsletter, attending meetups in your city, joining PythonistaCafe, going to PyCon, or becoming a member of a private "mastermind group"—

I *cannot* overstate how important it is to learn with and from other developers:

My programming skills, my business, and my personal life have all benefitted greatly from exchanging ideas and knowledge with others who truly cared about the same things as me.

It's impossible for me to keep up with the "information firehose" that is the Internet today. There's just too much interesting stuff happening day in and out.

I believe that the best strategy to deal with information overload is to rely on curation and a trusted group of peers you can share your journey with. It gives you a filter for the "new and shiny," and a powerful source of information you can tap into when you need it.

Happy Pythoning!

— Dan Bader

Older messages

Can I ask a favor?

Friday, November 20, 2020

Hey there, it's Dan... I noticed that you decided to pass on Managing Python Dependencies with Pip and Virtualenv—that's totally cool. I'm wondering if you could help me out by answering

time's up

Wednesday, November 18, 2020

Hey there, This is it— There's still time to save 20% on Managing Python Dependencies. But the bad news is, this deal is ending NOW. If you're still wondering if the course is right for you,

What Python developers say

Wednesday, November 18, 2020

Hey there, I wanted to share a few things that other Python developers have said about Managing Python Dependencies With Pip And Virtual Environments. Here's what the Python community had to say: ~

Q&A about Managing Python Dependencies

Tuesday, November 17, 2020

Hey there, After the announcement went out yesterday about the sale on my Managing Python Dependencies with Pip and Virtual Environments course, some of you emailed me with a bunch of questions about

[Python Mastery] What Pythonistas can learn from bestselling authors

Monday, November 16, 2020

Hey there, I just finished reading Stephen King's "On Writing." It's a great little book where he shares some of the writing advice and stories he's picked up over the course of

You Might Also Like

Import AI 399: 1,000 samples to make a reasoning model; DeepSeek proliferation; Apple's self-driving car simulator

Friday, February 14, 2025

What came before the golem? ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏

Defining Your Paranoia Level: Navigating Change Without the Overkill

Friday, February 14, 2025

We've all been there: trying to learn something new, only to find our old habits holding us back. We discussed today how our gut feelings about solving problems can sometimes be our own worst enemy

5 ways AI can help with taxes 🪄

Friday, February 14, 2025

Remotely control an iPhone; 💸 50+ early Presidents' Day deals -- ZDNET ZDNET Tech Today - US February 10, 2025 5 ways AI can help you with your taxes (and what not to use it for) 5 ways AI can help

Recurring Automations + Secret Updates

Friday, February 14, 2025

Smarter automations, better templates, and hidden updates to explore 👀 ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏

The First Provable AI-Proof Game: Introducing Butterfly Wings 4

Friday, February 14, 2025

Top Tech Content sent at Noon! Boost Your Article on HackerNoon for $159.99! Read this email in your browser How are you, @newsletterest1? undefined The Market Today #01 Instagram (Meta) 714.52 -0.32%

GCP Newsletter #437

Friday, February 14, 2025

Welcome to issue #437 February 10th, 2025 News BigQuery Cloud Marketplace Official Blog Partners BigQuery datasets now available on Google Cloud Marketplace - Google Cloud Marketplace now offers

Charted | The 1%'s Share of U.S. Wealth Over Time (1989-2024) 💰

Friday, February 14, 2025

Discover how the share of US wealth held by the top 1% has evolved from 1989 to 2024 in this infographic. View Online | Subscribe | Download Our App Download our app to see thousands of new charts from

The Great Social Media Diaspora & Tapestry is here

Friday, February 14, 2025

Apple introduces new app called 'Apple Invites', The Iconfactory launches Tapestry, beyond the traditional portfolio, and more in this week's issue of Creativerly. Creativerly The Great

Daily Coding Problem: Problem #1689 [Medium]

Friday, February 14, 2025

Daily Coding Problem Good morning! Here's your coding interview problem for today. This problem was asked by Google. Given a linked list, sort it in O(n log n) time and constant space. For example,

📧 Stop Conflating CQRS and MediatR

Friday, February 14, 2025

​ Stop Conflating CQRS and MediatR Read on: m​y website / Read time: 4 minutes The .NET Weekly is brought to you by: Step right up to the Generative AI Use Cases Repository! See how MongoDB powers your