Feature suggestion: math.zod for painlessly avoiding ZeroDivisionError

Terry Reedy tjreedy at udel.edu
Mon Apr 11 13:27:28 EDT 2011


On 4/11/2011 10:10 AM, Natan Yellin wrote:
> Hey everyone,
> This is my first posting to python-list, so be gentle.
>
> I propose the following function for the math module (which can, of
> course, be rewritten in C):
>
>       zod = lambda a, b: b and a / b

This is way too trivial to add.
>
> zod, the zero or divide function, is useful for division where the
> denominator can be 0. For example, here's one line of code
>     stat = x / y
> If y can be zero, that one-liner needs to be rewritten as:
>     if y != 0:
>          stat = x / y
>     else:
>          stat = 0

No. 'stat = y and x/y', which will be faster than 'stat = zod(x,y)', 
which does the same thing but adds a function call/

-- 
Terry Jan Reedy




More information about the Python-list mailing list