summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1993-10-27 09:28:23 (GMT)
committerGuido van Rossum <guido@python.org>1993-10-27 09:28:23 (GMT)
commitf1bbf9c308885648c2ecce611e975d21f220edf3 (patch)
treed6e6b2c6f4540ad15896c1a74e8e8ef8c7950312
parentb6957e434fab0b901cb2c8fafeba77f9ae7a653f (diff)
downloadcpython-f1bbf9c308885648c2ecce611e975d21f220edf3.zip
cpython-f1bbf9c308885648c2ecce611e975d21f220edf3.tar.gz
cpython-f1bbf9c308885648c2ecce611e975d21f220edf3.tar.bz2
Add coercions
-rwxr-xr-xDemo/classes/Rat.py14
1 files changed, 12 insertions, 2 deletions
diff --git a/Demo/classes/Rat.py b/Demo/classes/Rat.py
index 0b2519d..0d3e515 100755
--- a/Demo/classes/Rat.py
+++ b/Demo/classes/Rat.py
@@ -52,12 +52,18 @@ class Rat:
raise TypeError, 'Rat.__coerce__: bad other arg'
def __add__(a, b):
+ if type(b) <> type(a):
+ a, b = a.__coerce__(b)
+ return a + b
return rat(a.num*b.den + b.num*a.den, a.den*b.den)
def __sub__(a, b):
return rat(a.num*b.den - b.num*a.den, a.den*b.den)
def __mul__(a, b):
+ if type(b) <> type(a):
+ a, b = a.__coerce__(b)
+ return a * b
return rat(a.num*b.num, a.den*b.den)
def __div__(a, b):
@@ -78,9 +84,13 @@ def test():
l.sort()
print l
print rat(0, 1)
- print rat(1, 0)
print a+1
print a+1L
print a+1.0
+ try:
+ print rat(1, 0)
+ raise SystemError, 'should have been ZeroDivisionError'
+ except ZeroDivisionError:
+ print 'OK'
-test()
+#test()