unittest: Proposal to add failUnlessNear

Tim Peters tim.peters at gmail.com
Thu Jul 22 15:44:36 CEST 2004


[John Roth]
> Is that how complex subtraction works? I thought it produced
> a complex,

That's right.

> and your answers seem to say it produces a real.

No, complex.__abs__() produces a float.  Every type is free to
implement __abs__ in any way it likes.  complex.__abs__ is implemented
as if by

class complex:
   ...
   def __abs__(self):
       import math
       return math.hypot(self.real, self.imag)

In the same way, every type is free to implement __sub__ in any way it
likes, which is why infix "-" can also do a different thing for
complex operands than for float operands.

BTW, abs() itself is implemented as if by

def abs(x):
    return x.__abs__()



More information about the Python-list mailing list