overload builtin operator

Robert Kern rkern at ucsd.edu
Sat Aug 27 05:24:50 CEST 2005


Shaun wrote:
> Thanks for your replies, obviously this isn't a simple thing to do so
> I'll take a different tack.
> 
> The exact problem I am trying to solve here is to avoid the
> ZeroDivisionError in division.
> I have c++ code which delegates to python to calculate expressions on
> table cells. The values of the table cell are arbitary numbers and the
> expressions to be calculated are fairly simple python arithmetic and
> math functions.
> 
> The problem being that some users want an expression like '(100/x)+ 3'
> where x=0 to return 3. So that dividing a number by zero results in 0.

You have silly users. There's only one place I can think of where
division of a finite, nonzero number by zero can sensibly result in zero
(underdetermined least squares via SVD). I'd be curious as to why they
want that behavior.

> Apart from parsing the expression string myself and checking for divide
> by zero I can't find another way to solve the problem.

It might be easier to parse the expression and wrap all of the numbers
by a subclass of float that does division like you want.

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter




More information about the Python-list mailing list