diff options
author | Guido van Rossum <guido@python.org> | 1993-12-17 14:23:52 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1993-12-17 14:23:52 (GMT) |
commit | 7565b934144012f25e8b22d888572c048f0eb21a (patch) | |
tree | 1fe49e0e20f1201f97eb13596ef3c5f014d8182a /Demo/classes/Rat.py | |
parent | 3cbc16d9041fe1fa87356bdacc2f1ad52fb99a89 (diff) | |
download | cpython-7565b934144012f25e8b22d888572c048f0eb21a.zip cpython-7565b934144012f25e8b22d888572c048f0eb21a.tar.gz cpython-7565b934144012f25e8b22d888572c048f0eb21a.tar.bz2 |
*** empty log message ***
Diffstat (limited to 'Demo/classes/Rat.py')
-rwxr-xr-x | Demo/classes/Rat.py | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/Demo/classes/Rat.py b/Demo/classes/Rat.py index 0d3e515..d10e4df 100755 --- a/Demo/classes/Rat.py +++ b/Demo/classes/Rat.py @@ -2,7 +2,7 @@ def rat(num, den): - return Rat().init(num, den) + return Rat(num, den) def gcd(a, b): @@ -13,13 +13,12 @@ def gcd(a, b): class Rat: - def init(self, num, den): + def __init__(self, num, den): if den == 0: raise ZeroDivisionError, 'rat(x, 0)' g = gcd(num, den) self.num = num/g self.den = den/g - return self def __repr__(self): return 'rat' + `self.num, self.den` @@ -49,8 +48,10 @@ class Rat: return a, rat(b, 1L) if t == type(0.0): return a.__float__(), b + if t == type(a) and a.__class__ == b.__class__: + return a, b raise TypeError, 'Rat.__coerce__: bad other arg' - + def __add__(a, b): if type(b) <> type(a): a, b = a.__coerce__(b) @@ -93,4 +94,4 @@ def test(): except ZeroDivisionError: print 'OK' -#test() +test() |