Home Conference Sessions It Really is Eas...

It Really is Easier to Ask for Forgiveness (than Permission)

Naomi Ceder | GOTO Chicago 2019

You need to be signed in to add a collection

It's a Pythonic maxim that "it's easier to ask for forgiveness than permission" and Pythonic code is written with a strong preference for attempting an operation and raising exceptions as needed. This contrasts with many languages which take a "look before you leap" approach where conditions, types, and values are checked in advance. This tendency to raise exceptions first and ask questions later strikes coders from other languages (and even some Pythonistas) as reckless, strange, and inefficient. Wouldn't it be "better" to do a little checking and avoid exceptions? Exceptions can't be intended to be used so frequently, can they? Surely raising so many exceptions results in slow and inefficient code? This talk will address all of those concerns. We'll show how and why Python's approach is a strength, not a weakness, which makes Python more expressive and flexible. We'll also see that raising exceptions is integral to Python, and underlies many common features of the language. Finally, we'll look at some simple tests that show that the Pythonic use of exceptions usually is faster and more efficient than look before you leap checking in most cases, and we'll also discuss a few situations where "look before you leap" might be preferred. **Who should attend this talk:** People interested in how programming languages work and how they differ; people using Python who want to use it effectively; people who want to critique Python code and mentor effectively. **Academic level:** Introductory **What is the take away in this talk:** That a particular languages idioms and best practices are not what you might expect based on other languages; specifically that exceptions in Python should not be judged based on exceptions in Java, C++, or any other language.

Share on:
linkedin facebook
Copied!

Transcript

It's a Pythonic maxim that "it's easier to ask for forgiveness than permission" and Pythonic code is written with a strong preference for attempting an operation and raising exceptions as needed. This contrasts with many languages which take a "look before you leap" approach where conditions, types, and values are checked in advance.

This tendency to raise exceptions first and ask questions later strikes coders from other languages (and even some Pythonistas) as reckless, strange, and inefficient. Wouldn't it be "better" to do a little checking and avoid exceptions? Exceptions can't be intended to be used so frequently, can they? Surely raising so many exceptions results in slow and inefficient code?

This talk will address all of those concerns. We'll show how and why Python's approach is a strength, not a weakness, which makes Python more expressive and flexible. We'll also see that raising exceptions is integral to Python, and underlies many common features of the language. Finally, we'll look at some simple tests that show that the Pythonic use of exceptions usually is faster and more efficient than look before you leap checking in most cases, and we'll also discuss a few situations where "look before you leap" might be preferred.

Who should attend this talk: People interested in how programming languages work and how they differ; people using Python who want to use it effectively; people who want to critique Python code and mentor effectively.

Academic level: Introductory

What is the take away in this talk: That a particular languages idioms and best practices are not what you might expect based on other languages; specifically that exceptions in Python should not be judged based on exceptions in Java, C++, or any other language.

About the speakers

Naomi Ceder

Naomi Ceder

Chair of Python Software Foundation