1 # Copyright 2007 Google, Inc. All Rights Reserved.
2 # Licensed to PSF under a Contributor Agreement.
4 """Abstract Base Classes (ABCs) for numbers, according to PEP 3141.
6 TODO: Fill out more detailed documentation on the operators."""
8 from abc
import ABCMeta
, abstractmethod
, abstractproperty
10 __all__
= ["Number", "Complex", "Real", "Rational", "Integral"]
12 class Number(metaclass
=ABCMeta
):
13 """All numbers inherit from this class.
15 If you just want to check if an argument x is a number, without
16 caring what kind, use isinstance(x, Number).
18 # Concrete numeric types must provide their own hash implementation
24 ## Decimal has all of the methods specified by the Real abc, but it should
25 ## not be registered as a Real because decimals do not interoperate with
26 ## binary floats (i.e. Decimal('3.14') + 2.71828 is undefined). But,
27 ## abstract reals are expected to interoperate (i.e. R1 + R2 should be
28 ## expected to work if R1 and R2 are both Reals).
30 class Complex(Number
):
31 """Complex defines the operations that work on the builtin complex type.
33 In short, those are: a conversion to complex, .real, .imag, +, -,
34 *, /, abs(), .conjugate, ==, and !=.
36 If it is given heterogenous arguments, and doesn't have special
37 knowledge about them, it should fall back to the builtin complex
38 type as described below.
42 def __complex__(self
):
43 """Return a builtin complex instance. Called for complex(self)."""
46 """True if self != 0. Called for bool(self)."""
51 """Retrieve the real component of this number.
53 This should subclass Real.
55 raise NotImplementedError
59 """Retrieve the real component of this number.
61 This should subclass Real.
63 raise NotImplementedError
66 def __add__(self
, other
):
68 raise NotImplementedError
71 def __radd__(self
, other
):
73 raise NotImplementedError
78 raise NotImplementedError
83 raise NotImplementedError
85 def __sub__(self
, other
):
89 def __rsub__(self
, other
):
94 def __mul__(self
, other
):
96 raise NotImplementedError
99 def __rmul__(self
, other
):
101 raise NotImplementedError
104 def __truediv__(self
, other
):
105 """self / other: Should promote to float when necessary."""
106 raise NotImplementedError
109 def __rtruediv__(self
, other
):
111 raise NotImplementedError
114 def __pow__(self
, exponent
):
115 """self**exponent; should promote to float or complex when necessary."""
116 raise NotImplementedError
119 def __rpow__(self
, base
):
121 raise NotImplementedError
125 """Returns the Real distance from 0. Called for abs(self)."""
126 raise NotImplementedError
130 """(x+y*i).conjugate() returns (x-y*i)."""
131 raise NotImplementedError
134 def __eq__(self
, other
):
136 raise NotImplementedError
138 def __ne__(self
, other
):
140 # The default __ne__ doesn't negate __eq__ until 3.0.
141 return not (self
== other
)
143 Complex
.register(complex)
147 """To Complex, Real adds the operations that work on real numbers.
149 In short, those are: a conversion to float, trunc(), divmod,
152 Real also provides defaults for the derived operations.
157 """Any Real can be converted to a native float object.
159 Called for float(self)."""
160 raise NotImplementedError
164 """trunc(self): Truncates self to an Integral.
166 Returns an Integral i such that:
168 * abs(i) <= abs(self);
169 * for any Integral j satisfying the first two conditions,
170 abs(i) >= abs(j) [i.e. i has "maximal" abs among those].
171 i.e. "truncate towards 0".
173 raise NotImplementedError
177 """Finds the greatest Integral <= self."""
178 raise NotImplementedError
182 """Finds the least Integral >= self."""
183 raise NotImplementedError
186 def __round__(self
, ndigits
:"Integral"=None):
187 """Rounds self to ndigits decimal places, defaulting to 0.
189 If ndigits is omitted or None, returns an Integral, otherwise
190 returns a Real. Rounds half toward even.
192 raise NotImplementedError
194 def __divmod__(self
, other
):
195 """divmod(self, other): The pair (self // other, self % other).
197 Sometimes this can be computed faster than the pair of
200 return (self
// other
, self
% other
)
202 def __rdivmod__(self
, other
):
203 """divmod(other, self): The pair (self // other, self % other).
205 Sometimes this can be computed faster than the pair of
208 return (other
// self
, other
% self
)
211 def __floordiv__(self
, other
):
212 """self // other: The floor() of self/other."""
213 raise NotImplementedError
216 def __rfloordiv__(self
, other
):
217 """other // self: The floor() of other/self."""
218 raise NotImplementedError
221 def __mod__(self
, other
):
223 raise NotImplementedError
226 def __rmod__(self
, other
):
228 raise NotImplementedError
231 def __lt__(self
, other
):
234 < on Reals defines a total ordering, except perhaps for NaN."""
235 raise NotImplementedError
238 def __le__(self
, other
):
240 raise NotImplementedError
242 # Concrete implementations of Complex abstract methods.
243 def __complex__(self
):
244 """complex(self) == complex(float(self), 0)"""
245 return complex(float(self
))
249 """Real numbers are their real component."""
254 """Real numbers have no imaginary component."""
258 """Conjugate is a no-op for Reals."""
264 class Rational(Real
):
265 """.numerator and .denominator should be in lowest terms."""
269 raise NotImplementedError
272 def denominator(self
):
273 raise NotImplementedError
275 # Concrete implementation of Real's conversion to float.
277 """float(self) = self.numerator / self.denominator
279 It's important that this conversion use the integer's "true"
280 division rather than casting one side to float before dividing
281 so that ratios of huge integers convert without overflowing.
284 return self
.numerator
/ self
.denominator
287 class Integral(Rational
):
288 """Integral adds a conversion to int and the bit-string operations."""
293 raise NotImplementedError
300 def __pow__(self
, exponent
, modulus
=None):
301 """self ** exponent % modulus, but maybe faster.
303 Accept the modulus argument if you want to support the
304 3-argument version of pow(). Raise a TypeError if exponent < 0
305 or any argument isn't Integral. Otherwise, just implement the
306 2-argument version described in Complex.
308 raise NotImplementedError
311 def __lshift__(self
, other
):
313 raise NotImplementedError
316 def __rlshift__(self
, other
):
318 raise NotImplementedError
321 def __rshift__(self
, other
):
323 raise NotImplementedError
326 def __rrshift__(self
, other
):
328 raise NotImplementedError
331 def __and__(self
, other
):
333 raise NotImplementedError
336 def __rand__(self
, other
):
338 raise NotImplementedError
341 def __xor__(self
, other
):
343 raise NotImplementedError
346 def __rxor__(self
, other
):
348 raise NotImplementedError
351 def __or__(self
, other
):
353 raise NotImplementedError
356 def __ror__(self
, other
):
358 raise NotImplementedError
361 def __invert__(self
):
363 raise NotImplementedError
365 # Concrete implementations of Rational and Real abstract methods.
367 """float(self) == float(int(self))"""
368 return float(int(self
))
372 """Integers are their own numerators."""
376 def denominator(self
):
377 """Integers have a denominator of 1."""
380 Integral
.register(int)