The Journey From LBYL to EAFP * [Club]
Briefly

The Journey From LBYL to EAFP * [Club]
"LBYL came more naturally to me in my early years of programming. It seemed to have fewer obstacles in those early stages, fewer tricky concepts. And in my 10+ years of teaching Python, I also preferred teaching LBYL to beginners and delaying EAFP until later. But over the years, as I came to understand Python's psyche better, I gradually shifted my programming style-and then, my teaching style, too."
"You should definitely look before you leap across a busy road...or any road, really. And programming also has a Look Before You Leap concept-that's LBYL-when handling potential failure points in your code. Let's start by considering this basic example. You define a function that accepts a value and a list. The function adds the value to the list if the value is above a user-supplied threshold: def add_value_above_threshold(value, threshold, data): if value >= threshold: data.append(value) You can confirm this short code works as intended:"
LBYL means Look Before You Leap and often appeals to beginners because it uses explicit pre-checks instead of exceptions. EAFP stands for Easier to Ask Forgiveness than Permission and embraces exceptions as normal control flow in Python. A simple example shows add_value_above_threshold appending values above a threshold but raising AttributeError when given a dict. Teaching practice can move from LBYL to EAFP as understanding of Python's idioms deepens. Using EAFP reduces verbose pre-checking and aligns with Python's duck-typing philosophy. Three two-hour workshops cover dunder methods, iteration, and inheritance versus composition.
Read at Thepythoncodingstack
Unable to calculate read time
[
|
]