EAFP and LBYL (was: Try: rather than if :)

Ben Finney ben+python at benfinney.id.au
Mon Dec 14 18:19:38 EST 2015


Vincent Davis <vincent at vincentdavis.net> writes:

> In the code below try is used to check if handle has the attribute name. It
> seems an if statement could be used. Is there reason one way would be
> better than another?

The Python community refers to the difference by contrasting “look
before you leap” (LBYL) versus “easier to ask forgiveness than
permission” (EAFP), and tends to prefer the latter.

<URL:http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#eafp-vs-lbyl>

“It is easier to ask forgiveness than permission” is attributed to
computer programming legend Rear Admiral Grace Hopper (she who
documented the first actual computer bug — a large moth in the wires).

Alex Martelli explores when LBYL and EAFP are each appropriate in Python
<URL:http://pyvideo.org/video/1338/permission-or-forgiveness-0>.

-- 
 \       “The optimist thinks this is the best of all possible worlds. |
  `\           The pessimist fears it is true.” —J. Robert Oppenheimer |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list