Merged revisions 60475-60479,60481-60488 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60482 | raymond.hettinger | 2008-01-31 23:07:16 +0100 (Thu, 31 Jan 2008) | 1 line
Minor wordsmithing on docstring
........
r60483 | mark.dickinson | 2008-01-31 23:17:37 +0100 (Thu, 31 Jan 2008) | 5 lines
Issue #1678380. Fix a bug that identifies 0j and -0j when they appear
in the same code unit. The fix is essentially the same as the fix for a
previous bug identifying 0. and -0.
........
r60484 | christian.heimes | 2008-02-01 00:08:23 +0100 (Fri, 01 Feb 2008) | 1 line
Fixed bug #1983: Return from fork() is pid_t, not int
........
r60486 | jeffrey.yasskin | 2008-02-01 07:22:46 +0100 (Fri, 01 Feb 2008) | 4 lines
Move __builtins__.trunc() to math.trunc() per
http://mail.python.org/pipermail/python-dev/2008-January/076626.html and issue
1965.
........
r60487 | jeffrey.yasskin | 2008-02-01 08:05:46 +0100 (Fri, 01 Feb 2008) | 3 lines
Roll back r60248. It's useful to encourage users not to change Rational
instances.
........
r60488 | neal.norwitz | 2008-02-01 08:22:59 +0100 (Fri, 01 Feb 2008) | 1 line
Fix refleak
........
diff --git a/Lib/rational.py b/Lib/rational.py
index 06002a3..55d4a41 100755
--- a/Lib/rational.py
+++ b/Lib/rational.py
@@ -42,7 +42,7 @@
"""
- __slots__ = ('numerator', 'denominator')
+ __slots__ = ('_numerator', '_denominator')
# We're immutable, so use __new__ not __init__
def __new__(cls, numerator=0, denominator=1):
@@ -92,8 +92,8 @@
raise ZeroDivisionError('Rational(%s, 0)' % numerator)
g = gcd(numerator, denominator)
- self.numerator = int(numerator // g)
- self.denominator = int(denominator // g)
+ self._numerator = int(numerator // g)
+ self._denominator = int(denominator // g)
return self
@classmethod
@@ -167,6 +167,14 @@
result = new
return result
+ @property
+ def numerator(a):
+ return a._numerator
+
+ @property
+ def denominator(a):
+ return a._denominator
+
def __repr__(self):
"""repr(self)"""
return ('Rational(%r,%r)' % (self.numerator, self.denominator))
@@ -192,20 +200,20 @@
Rational, that means that we define __add__ and __radd__ as:
def __add__(self, other):
+ # Both types have numerators/denominator attributes,
+ # so do the operation directly
if isinstance(other, (int, Rational)):
- # Do the real operation.
return Rational(self.numerator * other.denominator +
other.numerator * self.denominator,
self.denominator * other.denominator)
- # float and complex don't follow this protocol, and
- # Rational knows about them, so special case them.
+ # float and complex don't have those operations, but we
+ # know about those types, so special case them.
elif isinstance(other, float):
return float(self) + other
elif isinstance(other, complex):
return complex(self) + other
- else:
- # Let the other type take over.
- return NotImplemented
+ # Let the other type take over.
+ return NotImplemented
def __radd__(self, other):
# radd handles more types than add because there's
@@ -218,8 +226,7 @@
return float(other) + float(self)
elif isinstance(other, Complex):
return complex(other) + complex(self)
- else:
- return NotImplemented
+ return NotImplemented
There are 5 different cases for a mixed-type addition on