Feature suggestion: math.zod for painlessly avoiding ZeroDivisionError

Natan Yellin aantny at gmail.com
Mon Apr 11 16:10:17 CEST 2011

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

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

...which is 4 lines of code for simple division!

Using zod, the zero or divide function, we can write:

> stat = zod(x,y)

I've encountered this issue before, but I don't know how common it is. Let
me know!
Would you use zod? Or do you need ood (one or divide), in which case lets
forget this altogether!


blog: http://natanyellin.com
<http://natanyellin.com/>twitter: @aantn <http://twitter.com/aantn>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110411/6e0eb88e/attachment.html>

More information about the Python-list mailing list