Monday Apr 29
16:20 –

It really is easier to ask for forgiveness (than permission)

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.

Naomi Ceder
Chair of Python Software Foundation
Organized by