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 | |
| 38 | If it is given heterogenous arguments, and doesn't have special |
| 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 | |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 144 | def __ne__(self, other): |
| 145 | """self != other""" |
| 146 | # The default __ne__ doesn't negate __eq__ until 3.0. |
| 147 | return not (self == other) |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 148 | |
| 149 | Complex.register(complex) |
| 150 | |
| 151 | |
| 152 | class Real(Complex): |
| 153 | """To Complex, Real adds the operations that work on real numbers. |
| 154 | |
| 155 | In short, those are: a conversion to float, trunc(), divmod, |
| 156 | %, <, <=, >, and >=. |
| 157 | |
| 158 | Real also provides defaults for the derived operations. |
| 159 | """ |
| 160 | |
Mark Dickinson | c28ad27 | 2009-02-12 17:58:36 +0000 | [diff] [blame] | 161 | __slots__ = () |
| 162 | |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 163 | @abstractmethod |
| 164 | def __float__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 165 | """Any Real can be converted to a native float object. |
| 166 | |
| 167 | Called for float(self).""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 168 | raise NotImplementedError |
| 169 | |
| 170 | @abstractmethod |
| 171 | def __trunc__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 172 | """trunc(self): Truncates self to an Integral. |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 173 | |
| 174 | Returns an Integral i such that: |
Guido van Rossum | d425630 | 2007-12-06 17:45:33 +0000 | [diff] [blame] | 175 | * i>0 iff self>0; |
| 176 | * abs(i) <= abs(self); |
| 177 | * for any Integral j satisfying the first two conditions, |
| 178 | abs(i) >= abs(j) [i.e. i has "maximal" abs among those]. |
| 179 | i.e. "truncate towards 0". |
| 180 | """ |
| 181 | raise NotImplementedError |
| 182 | |
| 183 | @abstractmethod |
| 184 | def __floor__(self): |
| 185 | """Finds the greatest Integral <= self.""" |
| 186 | raise NotImplementedError |
| 187 | |
| 188 | @abstractmethod |
| 189 | def __ceil__(self): |
| 190 | """Finds the least Integral >= self.""" |
| 191 | raise NotImplementedError |
| 192 | |
| 193 | @abstractmethod |
Raymond Hettinger | 219c300 | 2011-01-12 20:52:39 +0000 | [diff] [blame] | 194 | def __round__(self, ndigits=None): |
Guido van Rossum | d425630 | 2007-12-06 17:45:33 +0000 | [diff] [blame] | 195 | """Rounds self to ndigits decimal places, defaulting to 0. |
| 196 | |
| 197 | If ndigits is omitted or None, returns an Integral, otherwise |
| 198 | returns a Real. Rounds half toward even. |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 199 | """ |
| 200 | raise NotImplementedError |
| 201 | |
| 202 | def __divmod__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 203 | """divmod(self, other): The pair (self // other, self % other). |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 204 | |
| 205 | Sometimes this can be computed faster than the pair of |
| 206 | operations. |
| 207 | """ |
| 208 | return (self // other, self % other) |
| 209 | |
| 210 | def __rdivmod__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 211 | """divmod(other, self): The pair (self // other, self % other). |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 212 | |
| 213 | Sometimes this can be computed faster than the pair of |
| 214 | operations. |
| 215 | """ |
| 216 | return (other // self, other % self) |
| 217 | |
| 218 | @abstractmethod |
| 219 | def __floordiv__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 220 | """self // other: The floor() of self/other.""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 221 | raise NotImplementedError |
| 222 | |
| 223 | @abstractmethod |
| 224 | def __rfloordiv__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 225 | """other // self: The floor() of other/self.""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 226 | raise NotImplementedError |
| 227 | |
| 228 | @abstractmethod |
| 229 | def __mod__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 230 | """self % other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 231 | raise NotImplementedError |
| 232 | |
| 233 | @abstractmethod |
| 234 | def __rmod__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 235 | """other % self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 236 | raise NotImplementedError |
| 237 | |
| 238 | @abstractmethod |
| 239 | def __lt__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 240 | """self < other |
| 241 | |
| 242 | < on Reals defines a total ordering, except perhaps for NaN.""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 243 | raise NotImplementedError |
| 244 | |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 245 | @abstractmethod |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 246 | def __le__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 247 | """self <= other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 248 | raise NotImplementedError |
| 249 | |
| 250 | # Concrete implementations of Complex abstract methods. |
| 251 | def __complex__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 252 | """complex(self) == complex(float(self), 0)""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 253 | return complex(float(self)) |
| 254 | |
| 255 | @property |
| 256 | def real(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 257 | """Real numbers are their real component.""" |
Guido van Rossum | d425630 | 2007-12-06 17:45:33 +0000 | [diff] [blame] | 258 | return +self |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 259 | |
| 260 | @property |
| 261 | def imag(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 262 | """Real numbers have no imaginary component.""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 263 | return 0 |
| 264 | |
| 265 | def conjugate(self): |
| 266 | """Conjugate is a no-op for Reals.""" |
Guido van Rossum | d425630 | 2007-12-06 17:45:33 +0000 | [diff] [blame] | 267 | return +self |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 268 | |
| 269 | Real.register(float) |
| 270 | |
| 271 | |
Christian Heimes | 08976cb | 2008-03-16 00:32:36 +0000 | [diff] [blame] | 272 | class Rational(Real): |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 273 | """.numerator and .denominator should be in lowest terms.""" |
| 274 | |
Mark Dickinson | c28ad27 | 2009-02-12 17:58:36 +0000 | [diff] [blame] | 275 | __slots__ = () |
| 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 numerator(self): |
| 280 | raise NotImplementedError |
| 281 | |
Benjamin Peterson | bfebb7b | 2011-12-15 15:34:02 -0500 | [diff] [blame] | 282 | @property |
| 283 | @abstractmethod |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 284 | def denominator(self): |
| 285 | raise NotImplementedError |
| 286 | |
| 287 | # Concrete implementation of Real's conversion to float. |
| 288 | def __float__(self): |
Christian Heimes | 7b3ce6a | 2008-01-31 14:31:45 +0000 | [diff] [blame] | 289 | """float(self) = self.numerator / self.denominator |
| 290 | |
| 291 | It's important that this conversion use the integer's "true" |
| 292 | division rather than casting one side to float before dividing |
| 293 | so that ratios of huge integers convert without overflowing. |
| 294 | |
| 295 | """ |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 296 | return self.numerator / self.denominator |
| 297 | |
| 298 | |
| 299 | class Integral(Rational): |
| 300 | """Integral adds a conversion to int and the bit-string operations.""" |
| 301 | |
Mark Dickinson | c28ad27 | 2009-02-12 17:58:36 +0000 | [diff] [blame] | 302 | __slots__ = () |
| 303 | |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 304 | @abstractmethod |
| 305 | def __int__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 306 | """int(self)""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 307 | raise NotImplementedError |
| 308 | |
| 309 | def __index__(self): |
Éric Araujo | e2544bc | 2011-11-03 04:34:09 +0100 | [diff] [blame] | 310 | """Called whenever an index is needed, such as in slicing""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 311 | return int(self) |
| 312 | |
| 313 | @abstractmethod |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 314 | def __pow__(self, exponent, modulus=None): |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 315 | """self ** exponent % modulus, but maybe faster. |
| 316 | |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 317 | Accept the modulus argument if you want to support the |
| 318 | 3-argument version of pow(). Raise a TypeError if exponent < 0 |
| 319 | or any argument isn't Integral. Otherwise, just implement the |
| 320 | 2-argument version described in Complex. |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 321 | """ |
| 322 | raise NotImplementedError |
| 323 | |
| 324 | @abstractmethod |
| 325 | def __lshift__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 326 | """self << other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 327 | raise NotImplementedError |
| 328 | |
| 329 | @abstractmethod |
| 330 | def __rlshift__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 331 | """other << self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 332 | raise NotImplementedError |
| 333 | |
| 334 | @abstractmethod |
| 335 | def __rshift__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 336 | """self >> other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 337 | raise NotImplementedError |
| 338 | |
| 339 | @abstractmethod |
| 340 | def __rrshift__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 341 | """other >> self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 342 | raise NotImplementedError |
| 343 | |
| 344 | @abstractmethod |
| 345 | def __and__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 346 | """self & other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 347 | raise NotImplementedError |
| 348 | |
| 349 | @abstractmethod |
| 350 | def __rand__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 351 | """other & self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 352 | raise NotImplementedError |
| 353 | |
| 354 | @abstractmethod |
| 355 | def __xor__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 356 | """self ^ other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 357 | raise NotImplementedError |
| 358 | |
| 359 | @abstractmethod |
| 360 | def __rxor__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 361 | """other ^ self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 362 | raise NotImplementedError |
| 363 | |
| 364 | @abstractmethod |
| 365 | def __or__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 366 | """self | other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 367 | raise NotImplementedError |
| 368 | |
| 369 | @abstractmethod |
| 370 | def __ror__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 371 | """other | self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 372 | raise NotImplementedError |
| 373 | |
| 374 | @abstractmethod |
| 375 | def __invert__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 376 | """~self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 377 | raise NotImplementedError |
| 378 | |
| 379 | # Concrete implementations of Rational and Real abstract methods. |
| 380 | def __float__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 381 | """float(self) == float(int(self))""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 382 | return float(int(self)) |
| 383 | |
| 384 | @property |
| 385 | def numerator(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 386 | """Integers are their own numerators.""" |
Guido van Rossum | d425630 | 2007-12-06 17:45:33 +0000 | [diff] [blame] | 387 | return +self |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 388 | |
| 389 | @property |
| 390 | def denominator(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 391 | """Integers have a denominator of 1.""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 392 | return 1 |
| 393 | |
| 394 | Integral.register(int) |