diff options
author | Andrew M. Kuchling <amk@amk.ca> | 2003-04-24 17:04:45 (GMT) |
---|---|---|
committer | Andrew M. Kuchling <amk@amk.ca> | 2003-04-24 17:04:45 (GMT) |
commit | 4f237b6870bc856e2af5f23e524a9d32cd42e027 (patch) | |
tree | f01047df2d380d32bce83dce2638ae196e97fe41 /Demo | |
parent | 64b3c830d44aaeac8de85067113b03bc62a92957 (diff) | |
download | cpython-4f237b6870bc856e2af5f23e524a9d32cd42e027.zip cpython-4f237b6870bc856e2af5f23e524a9d32cd42e027.tar.gz cpython-4f237b6870bc856e2af5f23e524a9d32cd42e027.tar.bz2 |
Modernize code by using isinstance() instead of type() checks
Diffstat (limited to 'Demo')
-rwxr-xr-x | Demo/classes/Rat.py | 30 |
1 files changed, 14 insertions, 16 deletions
diff --git a/Demo/classes/Rat.py b/Demo/classes/Rat.py index 9e0c05d..b81f19f 100755 --- a/Demo/classes/Rat.py +++ b/Demo/classes/Rat.py @@ -28,10 +28,10 @@ def gcd(a, b): def rat(num, den = 1): # must check complex before float - if type(num) is ComplexType or type(den) is ComplexType: + if isinstance(num, complex) or isinstance(den, complex): # numerator or denominator is complex: return a complex return complex(num) / complex(den) - if type(num) is FloatType or type(den) is FloatType: + if isinstance(num, float) or isinstance(den, float): # numerator or denominator is float: return a float return float(num) / float(den) # otherwise return a rational @@ -47,28 +47,26 @@ class Rat: # normalize # must check complex before float - if type(num) is ComplexType or type(den) is ComplexType: + if (isinstance(num, complex) or + isinstance(den, complex)): # numerator or denominator is complex: # normalized form has denominator == 1+0j self.__num = complex(num) / complex(den) self.__den = complex(1) return - if type(num) is FloatType or type(den) is FloatType: + if isinstance(num, float) or isinstance(den, float): # numerator or denominator is float: # normalized form has denominator == 1.0 self.__num = float(num) / float(den) self.__den = 1.0 return - if (type(num) is InstanceType and - num.__class__ is self.__class__) or \ - (type(den) is InstanceType and - den.__class__ is self.__class__): + if (isinstance(num, self.__class__) or + isinstance(den, self.__class__)): # numerator or denominator is rational new = num / den - if type(new) is not InstanceType or \ - new.__class__ is not self.__class__: + if not isinstance(new, self.__class__): self.__num = new - if type(new) is ComplexType: + if isinstance(new, complex): self.__den = complex(1) else: self.__den = 1.0 @@ -165,11 +163,11 @@ class Rat: # a ** b def __pow__(a, b): if b.__den != 1: - if type(a.__num) is ComplexType: + if isinstance(a.__num, complex): a = complex(a) else: a = float(a) - if type(b.__num) is ComplexType: + if isinstance(b.__num, complex): b = complex(b) else: b = float(b) @@ -296,14 +294,14 @@ def test(): list = [2, 1.5, rat(3,2), 1.5+1.5j] for i in list: print i, - if type(i) is not ComplexType: + if not isinstance(i, complex): print int(i), float(i), print complex(i) print for j in list: print i + j, i - j, i * j, i / j, i ** j, - if not (isinstance(i, ComplexType) or - isinstance(j, ComplexType)): + if not (isinstance(i, complex) or + isinstance(j, complex)): print cmp(i, j) print |