LBYL vs EAFP

Ethan Furman ethan at stoneleaf.us
Mon Feb 4 19:26:37 EST 2013


On 02/04/2013 03:16 PM, Steven D'Aprano wrote:
> The eternal conflict between "Look Before You Leap" and "Easier to Ask for
> Forgiveness than Permission" (LBYL vs EAFP) continues...
>
> I want to check that a value is a number. Let's say I don't care what sort
> of number -- float, int, complex, Fraction, Decimal, something else -- just
> that it is a number. Should I:
>
> Look Before I Leap:
>
>      from numbers import Number
>      if isinstance(x, Number):
>          ...
>      else:
>          raise TypeError
>
>
> or Ask Forgiveness:
>
>      x + 0
>      ...

As Ian mentioned, both cases are LYBL, unless of course your addition 
was just an example of some mathematical code you have further down.

Personally, I go with EAFP unless I'm trying to present friendlier error 
messages.

~Ethan~



More information about the Python-list mailing list