Friday finking: TDD and EAFP

Ethan Furman ethan at stoneleaf.us
Sun Nov 3 23:28:21 EST 2019


On 10/31/2019 10:40 PM, DL Neil via Python-list wrote:

> Is the practice of TDD fundamentally, if not philosophically, somewhat contrary to Python's EAFP approach?

Not at all.  To use the maths example that's been going around:


def sqrt(number):
     ...
     return some_value


Your tests should do two things:
- verify the function works for good values
- verify the function fails for bad values

Your sqrt function does not need to test input types -- just go through the algorithm and return the result.  If an illegal operation occurs it will raise.

EAFP means the caller of your sqrt function can pass in whatever values they want (float, int, fraction, whatever) and either get a good result, or deal with the raised exception.

--
~Ethan~


More information about the Python-list mailing list