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

Issue 341 - Free supercharging and FSD offer extended for new Teslas

Thursday, November 28, 2024

View this email in your browser If you are just now finding out about Tesletter, you can subscribe here! If you already know Tesletter and want to support us, check out our Patreon page Issue 341 -

Programmer Weekly - Issue 233

Thursday, November 28, 2024

View this email in your browser Programmer Weekly Welcome to issue 233 of Programmer Weekly. Let's get straight to the links this week. Quote of the Week "All programmers are playwrights and

Python Weekly - Issue 678

Thursday, November 28, 2024

View this email in your browser Python Weekly Welcome to issue 678 of Python Weekly. Let's get straight to the links this week. Articles, Tutorials and Talks Let's build a AI Photo Generator

Web Tools #593 - Frameworks, React Tools, Uncats

Thursday, November 28, 2024

WEB VERSION Issue #593 • November 28, 2024 Advertisement Power Your Web Projects with Cloudways This BFCM As a developer, designer, or tech professional, you need reliable hosting that adapts to your

Python Weekly - Issue 678

Thursday, November 28, 2024

View this email in your browser Python Weekly Welcome to issue 678 of Python Weekly. Let's get straight to the links this week. Articles, Tutorials and Talks Let's build a AI Photo Generator

Introducing the Revamped HackerNoon AI Image Gallery

Thursday, November 28, 2024

Top Tech Content sent at Noon! Advertise on this Newsletter! 50% off!! Read this email in your browser How are you, @newsletterest1? 🪐 What's happening in tech today, November 28, 2024? The

Black Friday Giveaway @ Real Python

Thursday, November 28, 2024

Hey there, Just wanted to let you know that we're doing a special giveaway for Black Friday this week at Real Python 🎁🎉 You can get Python education resources worth over $1210, including three

Dispatch 019 🦃

Thursday, November 28, 2024

Watching the Parade • Paying for the Parade • Pirate TV Raid • US v. Microsoft Round 2 • Trump's Anti-Antitrust? • Google Anti-Antitrust The Spyglass Dispatch is a free newsletter sent out daily on

PHPWeekly November 28th 2024

Thursday, November 28, 2024

Curated news all about PHP. Here's the latest edition Is this email not displaying correctly? View it in your browser. PHP Weekly 28th November 2024 Hi everyone, It's Thanksgiving and Black

Widgets With Glance: Beyond String States

Thursday, November 28, 2024

View in browser 🔖 Articles Widgets With Glance: Beyond String States I had read about using a CustomGlanceStateDefinition but I couldn't find much about it in the official documentation so here is