24 May 2008

EAFP vs LBYL

I've been learning Python in the last few weeks. Many of my friends have switched to using it, making me think that it's the scientific programming language of the future (if not now).

I've been working my way through the official Python tutorial. I was amused to find the following in the tutorial glossary:
EAFP
Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements. The technique contrasts with the LBYL style that is common in many other languages such as C.
And what is LBYL, you may ask?
LBYL
Look before you leap. This coding style explicitly tests for pre-conditions before making calls or lookups. This style contrasts with the EAFP approach and is characterized by the presence of many if statements.

No comments:

Post a Comment