Graceful detection of EOF

Alex Martelli aleaxit at yahoo.com
Fri Oct 8 05:59:31 EDT 2004


Jeremy Jones <zanesdad at bellsouth.net> wrote:
   ...
> > This is well in line with the normal Python idiom,
> > as compared to "look before you leap".
> >
> >                 Andrew
> >                 dalke at dalkescientific.com
> 
> So, what you're saying is that the Python way, in contradistinction to
> "look before you leap", is "land in it, then wipe it off?"  Can we get
> that in the Zen of Python?  :-)

The "normal Python idiom" is often called, in honor and memory of
Admiral Grace Murray-Hopper (arguably the most significant woman in the
history of programming languages to this time), "it's Easier to Ask
Forgiveness than Permission" (EAFP, vs the LBYL alternative).  This
motto has been attributed to many, but Ms Hopper was undoubtedly the
first one to use it reportedly and in our field.

In the general case, trying to ascertain that an operation will succeed
before attempting the operation has many problems.  Often you end up
repeating the same steps between the ascertaining and the actual usage,
which offends the "Once and Only Once" principle as well as slowing
things down.  Sometimes you cannot ensure that the ascertaining and the
operating pertain to exactly the same thing -- the world can have
changed in-between, or the code might present subtle differences between
the two cases.

In contrast, if a failed attempt can be guaranteed to not alter
persistent state and only result in an easily catchable exception, EAFP
can better deliver on its name.  In terms of your analogy, there's
nothing to "wipe off" -- if the leap "misfires", no damage is done.


Alex
 



More information about the Python-list mailing list