[Python-checkins] python/dist/src/Demo/classes Complex.py,1.8,1.9

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Sat Apr 9 16:55:09 CEST 2005


Update of /cvsroot/python/python/dist/src/Demo/classes
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4402

Modified Files:
	Complex.py 
Log Message:
* Fix error in definition of Im() which returned self instead of 0
  for non-complex arguments.

* Replace type() comparisons with isinstance() checks.

* Replace apply() calls with equivalent syntactic form.

* Simplify __hash__ definition to hash the underlying tuple.

* Use math.hypot() for more robust computation of __abs__().

* Use sorted() instead of the multi-step keys/sort/iter.

* Update comment on the cmath module.



Index: Complex.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Demo/classes/Complex.py,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- Complex.py	9 Apr 2005 10:51:19 -0000	1.8
+++ Complex.py	9 Apr 2005 14:55:07 -0000	1.9
@@ -54,7 +54,7 @@
 # nor are shift and mask operations.
 #
 # The standard module math does not support complex numbers.
-# (I suppose it would be easy to implement a cmath module.)
+# The cmath modules should be used instead.
 #
 # Idea:
 # add a class Polar(r, phi) and mixed-mode arithmetic which
@@ -62,7 +62,7 @@
 # Complex for +,-,cmp
 # Polar   for *,/,pow
 
-import types, math
+import math
 import sys
 
 twopi = math.pi*2.0
@@ -74,8 +74,8 @@
 def ToComplex(obj):
     if IsComplex(obj):
         return obj
-    elif type(obj) == types.TupleType:
-        return apply(Complex, obj)
+    elif isinstance(obj, tuple):
+        return Complex(*obj)
     else:
         return Complex(obj)
 
@@ -86,14 +86,12 @@
 def Re(obj):
     if IsComplex(obj):
         return obj.re
-    else:
-        return obj
+    return obj
 
 def Im(obj):
     if IsComplex(obj):
         return obj.im
-    else:
-        return obj
+    return 0
 
 class Complex:
 
@@ -119,9 +117,9 @@
         raise TypeError, 'Complex numbers are immutable'
 
     def __hash__(self):
-        if not self.im: return hash(self.re)
-        mod = sys.maxint + 1L
-        return int((hash(self.re) + 2L*hash(self.im) + mod) % (2L*mod) - mod)
+        if not self.im:
+            return hash(self.re)
+        return hash((self.re, self.im))
 
     def __repr__(self):
         if not self.im:
@@ -142,8 +140,7 @@
         return self
 
     def __abs__(self):
-        # XXX could be done differently to avoid overflow!
-        return math.sqrt(self.re*self.re + self.im*self.im)
+        return math.hypot(self.re, self.im)
 
     def __int__(self):
         if self.im:
@@ -238,8 +235,8 @@
     except:
         result = sys.exc_type
     print '->', result
-    if (type(result) == type('') or type(value) == type('')):
-        ok = result == value
+    if isinstance(result, str) or isinstance(value, str):
+        ok = (result == value)
     else:
         ok = abs(result - value) <= fuzz
     if not ok:
@@ -312,13 +309,11 @@
                     (Complex(1), Complex(0,10), 1),
             ],
     }
-    exprs = testsuite.keys()
-    exprs.sort()
-    for expr in exprs:
+    for expr in sorted(testsuite):
         print expr + ':'
         t = (expr,)
         for item in testsuite[expr]:
-            apply(checkop, t+item)
+            checkop(*(t+item))
 
 
 if __name__ == '__main__':



More information about the Python-checkins mailing list