Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4f237b6

Browse files
committed
Modernize code by using isinstance() instead of type() checks
1 parent 64b3c83 commit 4f237b6

1 file changed

Lines changed: 14 additions & 16 deletions

File tree

Demo/classes/Rat.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ def gcd(a, b):
2828

2929
def rat(num, den = 1):
3030
# must check complex before float
31-
if type(num) is ComplexType or type(den) is ComplexType:
31+
if isinstance(num, complex) or isinstance(den, complex):
3232
# numerator or denominator is complex: return a complex
3333
return complex(num) / complex(den)
34-
if type(num) is FloatType or type(den) is FloatType:
34+
if isinstance(num, float) or isinstance(den, float):
3535
# numerator or denominator is float: return a float
3636
return float(num) / float(den)
3737
# otherwise return a rational
@@ -47,28 +47,26 @@ def __init__(self, num, den = 1):
4747
# normalize
4848

4949
# must check complex before float
50-
if type(num) is ComplexType or type(den) is ComplexType:
50+
if (isinstance(num, complex) or
51+
isinstance(den, complex)):
5152
# numerator or denominator is complex:
5253
# normalized form has denominator == 1+0j
5354
self.__num = complex(num) / complex(den)
5455
self.__den = complex(1)
5556
return
56-
if type(num) is FloatType or type(den) is FloatType:
57+
if isinstance(num, float) or isinstance(den, float):
5758
# numerator or denominator is float:
5859
# normalized form has denominator == 1.0
5960
self.__num = float(num) / float(den)
6061
self.__den = 1.0
6162
return
62-
if (type(num) is InstanceType and
63-
num.__class__ is self.__class__) or \
64-
(type(den) is InstanceType and
65-
den.__class__ is self.__class__):
63+
if (isinstance(num, self.__class__) or
64+
isinstance(den, self.__class__)):
6665
# numerator or denominator is rational
6766
new = num / den
68-
if type(new) is not InstanceType or \
69-
new.__class__ is not self.__class__:
67+
if not isinstance(new, self.__class__):
7068
self.__num = new
71-
if type(new) is ComplexType:
69+
if isinstance(new, complex):
7270
self.__den = complex(1)
7371
else:
7472
self.__den = 1.0
@@ -165,11 +163,11 @@ def __rmod__(b, a):
165163
# a ** b
166164
def __pow__(a, b):
167165
if b.__den != 1:
168-
if type(a.__num) is ComplexType:
166+
if isinstance(a.__num, complex):
169167
a = complex(a)
170168
else:
171169
a = float(a)
172-
if type(b.__num) is ComplexType:
170+
if isinstance(b.__num, complex):
173171
b = complex(b)
174172
else:
175173
b = float(b)
@@ -296,14 +294,14 @@ def test():
296294
list = [2, 1.5, rat(3,2), 1.5+1.5j]
297295
for i in list:
298296
print i,
299-
if type(i) is not ComplexType:
297+
if not isinstance(i, complex):
300298
print int(i), float(i),
301299
print complex(i)
302300
print
303301
for j in list:
304302
print i + j, i - j, i * j, i / j, i ** j,
305-
if not (isinstance(i, ComplexType) or
306-
isinstance(j, ComplexType)):
303+
if not (isinstance(i, complex) or
304+
isinstance(j, complex)):
307305
print cmp(i, j)
308306
print
309307

0 commit comments

Comments
 (0)