Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 1 | # Copyright 2007 Google, Inc. All Rights Reserved. |
| 2 | # Licensed to PSF under a Contributor Agreement. |
| 3 | |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 4 | """Abstract Base Classes (ABCs) for numbers, according to PEP 3141. |
| 5 | |
| 6 | TODO: Fill out more detailed documentation on the operators.""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 7 | |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 8 | from abc import ABCMeta, abstractmethod |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 9 | |
Christian Heimes | 08976cb | 2008-03-16 00:32:36 +0000 | [diff] [blame] | 10 | __all__ = ["Number", "Complex", "Real", "Rational", "Integral"] |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 11 | |
| 12 | class Number(metaclass=ABCMeta): |
| 13 | """All numbers inherit from this class. |
| 14 | |
| 15 | If you just want to check if an argument x is a number, without |
| 16 | caring what kind, use isinstance(x, Number). |
| 17 | """ |
Mark Dickinson | c28ad27 | 2009-02-12 17:58:36 +0000 | [diff] [blame] | 18 | __slots__ = () |
| 19 | |
Nick Coghlan | 36f4952 | 2008-08-18 12:31:52 +0000 | [diff] [blame] | 20 | # Concrete numeric types must provide their own hash implementation |
| 21 | __hash__ = None |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 22 | |
| 23 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 24 | ## Notes on Decimal |
| 25 | ## ---------------- |
| 26 | ## Decimal has all of the methods specified by the Real abc, but it should |
| 27 | ## not be registered as a Real because decimals do not interoperate with |
Christian Heimes | 08976cb | 2008-03-16 00:32:36 +0000 | [diff] [blame] | 28 | ## binary floats (i.e. Decimal('3.14') + 2.71828 is undefined). But, |
| 29 | ## abstract reals are expected to interoperate (i.e. R1 + R2 should be |
| 30 | ## expected to work if R1 and R2 are both Reals). |
Christian Heimes | 0bd4e11 | 2008-02-12 22:59:25 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 32 | class Complex(Number): |
| 33 | """Complex defines the operations that work on the builtin complex type. |
| 34 | |
| 35 | In short, those are: a conversion to complex, .real, .imag, +, -, |
| 36 | *, /, abs(), .conjugate, ==, and !=. |
| 37 | |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 38 | If it is given heterogeneous arguments, and doesn't have special |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 39 | knowledge about them, it should fall back to the builtin complex |
| 40 | type as described below. |
| 41 | """ |
| 42 | |
Mark Dickinson | c28ad27 | 2009-02-12 17:58:36 +0000 | [diff] [blame] | 43 | __slots__ = () |
| 44 | |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 45 | @abstractmethod |
| 46 | def __complex__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 47 | """Return a builtin complex instance. Called for complex(self).""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 48 | |
Jeffrey Yasskin | 9893de1 | 2008-01-17 07:36:30 +0000 | [diff] [blame] | 49 | def __bool__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 50 | """True if self != 0. Called for bool(self).""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 51 | return self != 0 |
| 52 | |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 53 | @property |
| 54 | @abstractmethod |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 55 | def real(self): |
| 56 | """Retrieve the real component of this number. |
| 57 | |
| 58 | This should subclass Real. |
| 59 | """ |
| 60 | raise NotImplementedError |
| 61 | |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 62 | @property |
| 63 | @abstractmethod |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 64 | def imag(self): |
Benjamin Peterson | 5793e6f | 2010-12-23 22:17:42 +0000 | [diff] [blame] | 65 | """Retrieve the imaginary component of this number. |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 66 | |
| 67 | This should subclass Real. |
| 68 | """ |
| 69 | raise NotImplementedError |
| 70 | |
| 71 | @abstractmethod |
| 72 | def __add__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 73 | """self + other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 74 | raise NotImplementedError |
| 75 | |
| 76 | @abstractmethod |
| 77 | def __radd__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 78 | """other + self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 79 | raise NotImplementedError |
| 80 | |
| 81 | @abstractmethod |
| 82 | def __neg__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 83 | """-self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 84 | raise NotImplementedError |
| 85 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 86 | @abstractmethod |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 87 | def __pos__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 88 | """+self""" |
Guido van Rossum | d425630 | 2007-12-06 17:45:33 +0000 | [diff] [blame] | 89 | raise NotImplementedError |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 90 | |
| 91 | def __sub__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 92 | """self - other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 93 | return self + -other |
| 94 | |
| 95 | def __rsub__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 96 | """other - self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 97 | return -self + other |
| 98 | |
| 99 | @abstractmethod |
| 100 | def __mul__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 101 | """self * other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 102 | raise NotImplementedError |
| 103 | |
| 104 | @abstractmethod |
| 105 | def __rmul__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 106 | """other * self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 107 | raise NotImplementedError |
| 108 | |
| 109 | @abstractmethod |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 110 | def __truediv__(self, other): |
Jeffrey Yasskin | 9893de1 | 2008-01-17 07:36:30 +0000 | [diff] [blame] | 111 | """self / other: Should promote to float when necessary.""" |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 112 | raise NotImplementedError |
| 113 | |
| 114 | @abstractmethod |
| 115 | def __rtruediv__(self, other): |
Jeffrey Yasskin | 9893de1 | 2008-01-17 07:36:30 +0000 | [diff] [blame] | 116 | """other / self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 117 | raise NotImplementedError |
| 118 | |
| 119 | @abstractmethod |
| 120 | def __pow__(self, exponent): |
Guido van Rossum | d425630 | 2007-12-06 17:45:33 +0000 | [diff] [blame] | 121 | """self**exponent; should promote to float or complex when necessary.""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 122 | raise NotImplementedError |
| 123 | |
| 124 | @abstractmethod |
| 125 | def __rpow__(self, base): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 126 | """base ** self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 127 | raise NotImplementedError |
| 128 | |
| 129 | @abstractmethod |
| 130 | def __abs__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 131 | """Returns the Real distance from 0. Called for abs(self).""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 132 | raise NotImplementedError |
| 133 | |
| 134 | @abstractmethod |
| 135 | def conjugate(self): |
| 136 | """(x+y*i).conjugate() returns (x-y*i).""" |
| 137 | raise NotImplementedError |
| 138 | |
| 139 | @abstractmethod |
| 140 | def __eq__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 141 | """self == other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 142 | raise NotImplementedError |
| 143 | |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 144 | Complex.register(complex) |
| 145 | |
| 146 | |
| 147 | class Real(Complex): |
| 148 | """To Complex, Real adds the operations that work on real numbers. |
| 149 | |
| 150 | In short, those are: a conversion to float, trunc(), divmod, |
Raymond Hettinger | 4e0ce82 | 2020-10-07 16:43:44 -0700 | [diff] [blame] | 151 | %, <, <=, >, and >=. |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 152 | |
| 153 | Real also provides defaults for the derived operations. |
| 154 | """ |
| 155 | |
Mark Dickinson | c28ad27 | 2009-02-12 17:58:36 +0000 | [diff] [blame] | 156 | __slots__ = () |
| 157 | |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 158 | @abstractmethod |
| 159 | def __float__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 160 | """Any Real can be converted to a native float object. |
| 161 | |
| 162 | Called for float(self).""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 163 | raise NotImplementedError |
| 164 | |
| 165 | @abstractmethod |
| 166 | def __trunc__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 167 | """trunc(self): Truncates self to an Integral. |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 168 | |
| 169 | Returns an Integral i such that: |
Guido van Rossum | d425630 | 2007-12-06 17:45:33 +0000 | [diff] [blame] | 170 | * i>0 iff self>0; |
| 171 | * abs(i) <= abs(self); |
| 172 | * for any Integral j satisfying the first two conditions, |
| 173 | abs(i) >= abs(j) [i.e. i has "maximal" abs among those]. |
| 174 | i.e. "truncate towards 0". |
| 175 | """ |
| 176 | raise NotImplementedError |
| 177 | |
| 178 | @abstractmethod |
| 179 | def __floor__(self): |
| 180 | """Finds the greatest Integral <= self.""" |
| 181 | raise NotImplementedError |
| 182 | |
| 183 | @abstractmethod |
| 184 | def __ceil__(self): |
| 185 | """Finds the least Integral >= self.""" |
| 186 | raise NotImplementedError |
| 187 | |
| 188 | @abstractmethod |
Raymond Hettinger | 219c300 | 2011-01-12 20:52:39 +0000 | [diff] [blame] | 189 | def __round__(self, ndigits=None): |
Guido van Rossum | d425630 | 2007-12-06 17:45:33 +0000 | [diff] [blame] | 190 | """Rounds self to ndigits decimal places, defaulting to 0. |
| 191 | |
| 192 | If ndigits is omitted or None, returns an Integral, otherwise |
| 193 | returns a Real. Rounds half toward even. |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 194 | """ |
| 195 | raise NotImplementedError |
| 196 | |
| 197 | def __divmod__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 198 | """divmod(self, other): The pair (self // other, self % other). |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 199 | |
| 200 | Sometimes this can be computed faster than the pair of |
| 201 | operations. |
| 202 | """ |
| 203 | return (self // other, self % other) |
| 204 | |
| 205 | def __rdivmod__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 206 | """divmod(other, self): The pair (self // other, self % other). |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 207 | |
| 208 | Sometimes this can be computed faster than the pair of |
| 209 | operations. |
| 210 | """ |
| 211 | return (other // self, other % self) |
| 212 | |
| 213 | @abstractmethod |
| 214 | def __floordiv__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 215 | """self // other: The floor() of self/other.""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 216 | raise NotImplementedError |
| 217 | |
| 218 | @abstractmethod |
| 219 | def __rfloordiv__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 220 | """other // self: The floor() of other/self.""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 221 | raise NotImplementedError |
| 222 | |
| 223 | @abstractmethod |
| 224 | def __mod__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 225 | """self % other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 226 | raise NotImplementedError |
| 227 | |
| 228 | @abstractmethod |
| 229 | def __rmod__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 230 | """other % self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 231 | raise NotImplementedError |
| 232 | |
| 233 | @abstractmethod |
| 234 | def __lt__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 235 | """self < other |
| 236 | |
| 237 | < on Reals defines a total ordering, except perhaps for NaN.""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 238 | raise NotImplementedError |
| 239 | |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 240 | @abstractmethod |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 241 | def __le__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 242 | """self <= other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 243 | raise NotImplementedError |
| 244 | |
| 245 | # Concrete implementations of Complex abstract methods. |
| 246 | def __complex__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 247 | """complex(self) == complex(float(self), 0)""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 248 | return complex(float(self)) |
| 249 | |
| 250 | @property |
| 251 | def real(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 252 | """Real numbers are their real component.""" |
Guido van Rossum | d425630 | 2007-12-06 17:45:33 +0000 | [diff] [blame] | 253 | return +self |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 254 | |
| 255 | @property |
| 256 | def imag(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 257 | """Real numbers have no imaginary component.""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 258 | return 0 |
| 259 | |
| 260 | def conjugate(self): |
| 261 | """Conjugate is a no-op for Reals.""" |
Guido van Rossum | d425630 | 2007-12-06 17:45:33 +0000 | [diff] [blame] | 262 | return +self |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 263 | |
| 264 | Real.register(float) |
| 265 | |
| 266 | |
Christian Heimes | 08976cb | 2008-03-16 00:32:36 +0000 | [diff] [blame] | 267 | class Rational(Real): |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 268 | """.numerator and .denominator should be in lowest terms.""" |
| 269 | |
Mark Dickinson | c28ad27 | 2009-02-12 17:58:36 +0000 | [diff] [blame] | 270 | __slots__ = () |
| 271 | |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 272 | @property |
| 273 | @abstractmethod |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 274 | def numerator(self): |
| 275 | raise NotImplementedError |
| 276 | |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 277 | @property |
| 278 | @abstractmethod |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 279 | def denominator(self): |
| 280 | raise NotImplementedError |
| 281 | |
| 282 | # Concrete implementation of Real's conversion to float. |
| 283 | def __float__(self): |
Christian Heimes | 7b3ce6a | 2008-01-31 14:31:45 +0000 | [diff] [blame] | 284 | """float(self) = self.numerator / self.denominator |
| 285 | |
| 286 | It's important that this conversion use the integer's "true" |
| 287 | division rather than casting one side to float before dividing |
| 288 | so that ratios of huge integers convert without overflowing. |
| 289 | |
| 290 | """ |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 291 | return self.numerator / self.denominator |
| 292 | |
| 293 | |
| 294 | class Integral(Rational): |
| 295 | """Integral adds a conversion to int and the bit-string operations.""" |
| 296 | |
Mark Dickinson | c28ad27 | 2009-02-12 17:58:36 +0000 | [diff] [blame] | 297 | __slots__ = () |
| 298 | |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 299 | @abstractmethod |
| 300 | def __int__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 301 | """int(self)""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 302 | raise NotImplementedError |
| 303 | |
| 304 | def __index__(self): |
Éric Araujo | e2544bc | 2011-11-03 04:34:09 +0100 | [diff] [blame] | 305 | """Called whenever an index is needed, such as in slicing""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 306 | return int(self) |
| 307 | |
| 308 | @abstractmethod |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 309 | def __pow__(self, exponent, modulus=None): |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 310 | """self ** exponent % modulus, but maybe faster. |
| 311 | |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 312 | Accept the modulus argument if you want to support the |
| 313 | 3-argument version of pow(). Raise a TypeError if exponent < 0 |
| 314 | or any argument isn't Integral. Otherwise, just implement the |
| 315 | 2-argument version described in Complex. |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 316 | """ |
| 317 | raise NotImplementedError |
| 318 | |
| 319 | @abstractmethod |
| 320 | def __lshift__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 321 | """self << other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 322 | raise NotImplementedError |
| 323 | |
| 324 | @abstractmethod |
| 325 | def __rlshift__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 326 | """other << self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 327 | raise NotImplementedError |
| 328 | |
| 329 | @abstractmethod |
| 330 | def __rshift__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 331 | """self >> other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 332 | raise NotImplementedError |
| 333 | |
| 334 | @abstractmethod |
| 335 | def __rrshift__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 336 | """other >> self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 337 | raise NotImplementedError |
| 338 | |
| 339 | @abstractmethod |
| 340 | def __and__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 341 | """self & other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 342 | raise NotImplementedError |
| 343 | |
| 344 | @abstractmethod |
| 345 | def __rand__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 346 | """other & self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 347 | raise NotImplementedError |
| 348 | |
| 349 | @abstractmethod |
| 350 | def __xor__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 351 | """self ^ other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 352 | raise NotImplementedError |
| 353 | |
| 354 | @abstractmethod |
| 355 | def __rxor__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 356 | """other ^ self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 357 | raise NotImplementedError |
| 358 | |
| 359 | @abstractmethod |
| 360 | def __or__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 361 | """self | other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 362 | raise NotImplementedError |
| 363 | |
| 364 | @abstractmethod |
| 365 | def __ror__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 366 | """other | self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 367 | raise NotImplementedError |
| 368 | |
| 369 | @abstractmethod |
| 370 | def __invert__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 371 | """~self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 372 | raise NotImplementedError |
| 373 | |
| 374 | # Concrete implementations of Rational and Real abstract methods. |
| 375 | def __float__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 376 | """float(self) == float(int(self))""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 377 | return float(int(self)) |
| 378 | |
| 379 | @property |
| 380 | def numerator(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 381 | """Integers are their own numerators.""" |
Guido van Rossum | d425630 | 2007-12-06 17:45:33 +0000 | [diff] [blame] | 382 | return +self |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 383 | |
| 384 | @property |
| 385 | def denominator(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 386 | """Integers have a denominator of 1.""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 387 | return 1 |
| 388 | |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 389 | Integral.register(int) |