summaryrefslogtreecommitdiffstats
path: root/Lib/numbers.py
diff options
context:
space:
mode:
authorJeffrey Yasskin <jyasskin@gmail.com>2008-01-15 07:46:24 (GMT)
committerJeffrey Yasskin <jyasskin@gmail.com>2008-01-15 07:46:24 (GMT)
commitd7b00334f3cbf7a802e875238b9f2bd95e190436 (patch)
tree0324043740339278109491f3c7afef3be6b1a425 /Lib/numbers.py
parentca9c6e433c6637352eecbe3432786a1ae9bec1de (diff)
downloadcpython-d7b00334f3cbf7a802e875238b9f2bd95e190436.zip
cpython-d7b00334f3cbf7a802e875238b9f2bd95e190436.tar.gz
cpython-d7b00334f3cbf7a802e875238b9f2bd95e190436.tar.bz2
Add rational.Rational as an implementation of numbers.Rational with infinite
precision. This has been discussed at http://bugs.python.org/issue1682. It's useful primarily for teaching, but it also demonstrates how to implement a member of the numeric tower, including fallbacks for mixed-mode arithmetic. I expect to write a couple more patches in this area: * Rational.from_decimal() * Rational.trim/approximate() (maybe with different names) * Maybe remove the parentheses from Rational.__str__() * Maybe rename one of the Rational classes * Maybe make Rational('3/2') work.
Diffstat (limited to 'Lib/numbers.py')
-rw-r--r--Lib/numbers.py25
1 files changed, 22 insertions, 3 deletions
diff --git a/Lib/numbers.py b/Lib/numbers.py
index 3c13290..8e02203 100644
--- a/Lib/numbers.py
+++ b/Lib/numbers.py
@@ -5,6 +5,7 @@
TODO: Fill out more detailed documentation on the operators."""
+from __future__ import division
from abc import ABCMeta, abstractmethod, abstractproperty
__all__ = ["Number", "Exact", "Inexact",
@@ -63,7 +64,8 @@ class Complex(Number):
def __complex__(self):
"""Return a builtin complex instance. Called for complex(self)."""
- def __bool__(self):
+ # Will be __bool__ in 3.0.
+ def __nonzero__(self):
"""True if self != 0. Called for bool(self)."""
return self != 0
@@ -98,6 +100,7 @@ class Complex(Number):
"""-self"""
raise NotImplementedError
+ @abstractmethod
def __pos__(self):
"""+self"""
raise NotImplementedError
@@ -122,12 +125,28 @@ class Complex(Number):
@abstractmethod
def __div__(self, other):
- """self / other; should promote to float or complex when necessary."""
+ """self / other without __future__ division
+
+ May promote to float.
+ """
raise NotImplementedError
@abstractmethod
def __rdiv__(self, other):
- """other / self"""
+ """other / self without __future__ division"""
+ raise NotImplementedError
+
+ @abstractmethod
+ def __truediv__(self, other):
+ """self / other with __future__ division.
+
+ Should promote to float when necessary.
+ """
+ raise NotImplementedError
+
+ @abstractmethod
+ def __rtruediv__(self, other):
+ """other / self with __future__ division"""
raise NotImplementedError
@abstractmethod