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 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 8 | from __future__ import division |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 9 | from abc import ABCMeta, abstractmethod, abstractproperty |
| 10 | |
| 11 | __all__ = ["Number", "Exact", "Inexact", |
| 12 | "Complex", "Real", "Rational", "Integral", |
| 13 | ] |
| 14 | |
| 15 | |
| 16 | class Number(metaclass=ABCMeta): |
| 17 | """All numbers inherit from this class. |
| 18 | |
| 19 | If you just want to check if an argument x is a number, without |
| 20 | caring what kind, use isinstance(x, Number). |
| 21 | """ |
| 22 | |
| 23 | |
| 24 | class Exact(Number): |
| 25 | """Operations on instances of this type are exact. |
| 26 | |
| 27 | As long as the result of a homogenous operation is of the same |
| 28 | type, you can assume that it was computed exactly, and there are |
| 29 | no round-off errors. Laws like commutativity and associativity |
| 30 | hold. |
| 31 | """ |
| 32 | |
| 33 | Exact.register(int) |
| 34 | |
| 35 | |
| 36 | class Inexact(Number): |
| 37 | """Operations on instances of this type are inexact. |
| 38 | |
| 39 | Given X, an instance of Inexact, it is possible that (X + -X) + 3 |
| 40 | == 3, but X + (-X + 3) == 0. The exact form this error takes will |
| 41 | vary by type, but it's generally unsafe to compare this type for |
| 42 | equality. |
| 43 | """ |
| 44 | |
| 45 | Inexact.register(complex) |
| 46 | Inexact.register(float) |
Guido van Rossum | d425630 | 2007-12-06 17:45:33 +0000 | [diff] [blame] | 47 | # Inexact.register(decimal.Decimal) |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | class Complex(Number): |
| 51 | """Complex defines the operations that work on the builtin complex type. |
| 52 | |
| 53 | In short, those are: a conversion to complex, .real, .imag, +, -, |
| 54 | *, /, abs(), .conjugate, ==, and !=. |
| 55 | |
| 56 | If it is given heterogenous arguments, and doesn't have special |
| 57 | knowledge about them, it should fall back to the builtin complex |
| 58 | type as described below. |
| 59 | """ |
| 60 | |
| 61 | @abstractmethod |
| 62 | def __complex__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 63 | """Return a builtin complex instance. Called for complex(self).""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 65 | # Will be __bool__ in 3.0. |
| 66 | def __nonzero__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 67 | """True if self != 0. Called for bool(self).""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 68 | return self != 0 |
| 69 | |
| 70 | @abstractproperty |
| 71 | def real(self): |
| 72 | """Retrieve the real component of this number. |
| 73 | |
| 74 | This should subclass Real. |
| 75 | """ |
| 76 | raise NotImplementedError |
| 77 | |
| 78 | @abstractproperty |
| 79 | def imag(self): |
| 80 | """Retrieve the real component of this number. |
| 81 | |
| 82 | This should subclass Real. |
| 83 | """ |
| 84 | raise NotImplementedError |
| 85 | |
| 86 | @abstractmethod |
| 87 | def __add__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 88 | """self + other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 89 | raise NotImplementedError |
| 90 | |
| 91 | @abstractmethod |
| 92 | def __radd__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 93 | """other + self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 94 | raise NotImplementedError |
| 95 | |
| 96 | @abstractmethod |
| 97 | def __neg__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 98 | """-self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 99 | raise NotImplementedError |
| 100 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 101 | @abstractmethod |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 102 | def __pos__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 103 | """+self""" |
Guido van Rossum | d425630 | 2007-12-06 17:45:33 +0000 | [diff] [blame] | 104 | raise NotImplementedError |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 105 | |
| 106 | def __sub__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 107 | """self - other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 108 | return self + -other |
| 109 | |
| 110 | def __rsub__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 111 | """other - self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 112 | return -self + other |
| 113 | |
| 114 | @abstractmethod |
| 115 | def __mul__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 116 | """self * other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 117 | raise NotImplementedError |
| 118 | |
| 119 | @abstractmethod |
| 120 | def __rmul__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 121 | """other * self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 122 | raise NotImplementedError |
| 123 | |
| 124 | @abstractmethod |
| 125 | def __div__(self, other): |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 126 | """self / other without __future__ division |
| 127 | |
| 128 | May promote to float. |
| 129 | """ |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 130 | raise NotImplementedError |
| 131 | |
| 132 | @abstractmethod |
| 133 | def __rdiv__(self, other): |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 134 | """other / self without __future__ division""" |
| 135 | raise NotImplementedError |
| 136 | |
| 137 | @abstractmethod |
| 138 | def __truediv__(self, other): |
| 139 | """self / other with __future__ division. |
| 140 | |
| 141 | Should promote to float when necessary. |
| 142 | """ |
| 143 | raise NotImplementedError |
| 144 | |
| 145 | @abstractmethod |
| 146 | def __rtruediv__(self, other): |
| 147 | """other / self with __future__ division""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 148 | raise NotImplementedError |
| 149 | |
| 150 | @abstractmethod |
| 151 | def __pow__(self, exponent): |
Guido van Rossum | d425630 | 2007-12-06 17:45:33 +0000 | [diff] [blame] | 152 | """self**exponent; should promote to float or complex when necessary.""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 153 | raise NotImplementedError |
| 154 | |
| 155 | @abstractmethod |
| 156 | def __rpow__(self, base): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 157 | """base ** self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 158 | raise NotImplementedError |
| 159 | |
| 160 | @abstractmethod |
| 161 | def __abs__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 162 | """Returns the Real distance from 0. Called for abs(self).""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 163 | raise NotImplementedError |
| 164 | |
| 165 | @abstractmethod |
| 166 | def conjugate(self): |
| 167 | """(x+y*i).conjugate() returns (x-y*i).""" |
| 168 | raise NotImplementedError |
| 169 | |
| 170 | @abstractmethod |
| 171 | def __eq__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 172 | """self == other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 173 | raise NotImplementedError |
| 174 | |
Guido van Rossum | d425630 | 2007-12-06 17:45:33 +0000 | [diff] [blame] | 175 | # __ne__ is inherited from object and negates whatever __eq__ does. |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 176 | |
| 177 | Complex.register(complex) |
| 178 | |
| 179 | |
| 180 | class Real(Complex): |
| 181 | """To Complex, Real adds the operations that work on real numbers. |
| 182 | |
| 183 | In short, those are: a conversion to float, trunc(), divmod, |
| 184 | %, <, <=, >, and >=. |
| 185 | |
| 186 | Real also provides defaults for the derived operations. |
| 187 | """ |
| 188 | |
| 189 | @abstractmethod |
| 190 | def __float__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 191 | """Any Real can be converted to a native float object. |
| 192 | |
| 193 | Called for float(self).""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 194 | raise NotImplementedError |
| 195 | |
| 196 | @abstractmethod |
| 197 | def __trunc__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 198 | """trunc(self): Truncates self to an Integral. |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 199 | |
| 200 | Returns an Integral i such that: |
Guido van Rossum | d425630 | 2007-12-06 17:45:33 +0000 | [diff] [blame] | 201 | * i>0 iff self>0; |
| 202 | * abs(i) <= abs(self); |
| 203 | * for any Integral j satisfying the first two conditions, |
| 204 | abs(i) >= abs(j) [i.e. i has "maximal" abs among those]. |
| 205 | i.e. "truncate towards 0". |
| 206 | """ |
| 207 | raise NotImplementedError |
| 208 | |
| 209 | @abstractmethod |
| 210 | def __floor__(self): |
| 211 | """Finds the greatest Integral <= self.""" |
| 212 | raise NotImplementedError |
| 213 | |
| 214 | @abstractmethod |
| 215 | def __ceil__(self): |
| 216 | """Finds the least Integral >= self.""" |
| 217 | raise NotImplementedError |
| 218 | |
| 219 | @abstractmethod |
| 220 | def __round__(self, ndigits:"Integral"=None): |
| 221 | """Rounds self to ndigits decimal places, defaulting to 0. |
| 222 | |
| 223 | If ndigits is omitted or None, returns an Integral, otherwise |
| 224 | returns a Real. Rounds half toward even. |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 225 | """ |
| 226 | raise NotImplementedError |
| 227 | |
| 228 | def __divmod__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 229 | """divmod(self, other): The pair (self // other, self % other). |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 230 | |
| 231 | Sometimes this can be computed faster than the pair of |
| 232 | operations. |
| 233 | """ |
| 234 | return (self // other, self % other) |
| 235 | |
| 236 | def __rdivmod__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 237 | """divmod(other, self): The pair (self // other, self % other). |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 238 | |
| 239 | Sometimes this can be computed faster than the pair of |
| 240 | operations. |
| 241 | """ |
| 242 | return (other // self, other % self) |
| 243 | |
| 244 | @abstractmethod |
| 245 | def __floordiv__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 246 | """self // other: The floor() of self/other.""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 247 | raise NotImplementedError |
| 248 | |
| 249 | @abstractmethod |
| 250 | def __rfloordiv__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 251 | """other // self: The floor() of other/self.""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 252 | raise NotImplementedError |
| 253 | |
| 254 | @abstractmethod |
| 255 | def __mod__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 256 | """self % other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 257 | raise NotImplementedError |
| 258 | |
| 259 | @abstractmethod |
| 260 | def __rmod__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 261 | """other % self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 262 | raise NotImplementedError |
| 263 | |
| 264 | @abstractmethod |
| 265 | def __lt__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 266 | """self < other |
| 267 | |
| 268 | < on Reals defines a total ordering, except perhaps for NaN.""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 269 | raise NotImplementedError |
| 270 | |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 271 | @abstractmethod |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 272 | def __le__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 273 | """self <= other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 274 | raise NotImplementedError |
| 275 | |
| 276 | # Concrete implementations of Complex abstract methods. |
| 277 | def __complex__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 278 | """complex(self) == complex(float(self), 0)""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 279 | return complex(float(self)) |
| 280 | |
| 281 | @property |
| 282 | def real(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 283 | """Real numbers are their real component.""" |
Guido van Rossum | d425630 | 2007-12-06 17:45:33 +0000 | [diff] [blame] | 284 | return +self |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 285 | |
| 286 | @property |
| 287 | def imag(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 288 | """Real numbers have no imaginary component.""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 289 | return 0 |
| 290 | |
| 291 | def conjugate(self): |
| 292 | """Conjugate is a no-op for Reals.""" |
Guido van Rossum | d425630 | 2007-12-06 17:45:33 +0000 | [diff] [blame] | 293 | return +self |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 294 | |
| 295 | Real.register(float) |
Guido van Rossum | d425630 | 2007-12-06 17:45:33 +0000 | [diff] [blame] | 296 | # Real.register(decimal.Decimal) |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 297 | |
| 298 | |
| 299 | class Rational(Real, Exact): |
| 300 | """.numerator and .denominator should be in lowest terms.""" |
| 301 | |
| 302 | @abstractproperty |
| 303 | def numerator(self): |
| 304 | raise NotImplementedError |
| 305 | |
| 306 | @abstractproperty |
| 307 | def denominator(self): |
| 308 | raise NotImplementedError |
| 309 | |
| 310 | # Concrete implementation of Real's conversion to float. |
| 311 | def __float__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 312 | """float(self) = self.numerator / self.denominator""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 313 | return self.numerator / self.denominator |
| 314 | |
| 315 | |
| 316 | class Integral(Rational): |
| 317 | """Integral adds a conversion to int and the bit-string operations.""" |
| 318 | |
| 319 | @abstractmethod |
| 320 | def __int__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 321 | """int(self)""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 322 | raise NotImplementedError |
| 323 | |
| 324 | def __index__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 325 | """index(self)""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 326 | return int(self) |
| 327 | |
| 328 | @abstractmethod |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 329 | def __pow__(self, exponent, modulus=None): |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 330 | """self ** exponent % modulus, but maybe faster. |
| 331 | |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 332 | Accept the modulus argument if you want to support the |
| 333 | 3-argument version of pow(). Raise a TypeError if exponent < 0 |
| 334 | or any argument isn't Integral. Otherwise, just implement the |
| 335 | 2-argument version described in Complex. |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 336 | """ |
| 337 | raise NotImplementedError |
| 338 | |
| 339 | @abstractmethod |
| 340 | def __lshift__(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 __rlshift__(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 __rshift__(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 __rrshift__(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 __and__(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 __rand__(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 __xor__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 371 | """self ^ other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 372 | raise NotImplementedError |
| 373 | |
| 374 | @abstractmethod |
| 375 | def __rxor__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 376 | """other ^ self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 377 | raise NotImplementedError |
| 378 | |
| 379 | @abstractmethod |
| 380 | def __or__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 381 | """self | other""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 382 | raise NotImplementedError |
| 383 | |
| 384 | @abstractmethod |
| 385 | def __ror__(self, other): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 386 | """other | self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 387 | raise NotImplementedError |
| 388 | |
| 389 | @abstractmethod |
| 390 | def __invert__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 391 | """~self""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 392 | raise NotImplementedError |
| 393 | |
| 394 | # Concrete implementations of Rational and Real abstract methods. |
| 395 | def __float__(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 396 | """float(self) == float(int(self))""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 397 | return float(int(self)) |
| 398 | |
| 399 | @property |
| 400 | def numerator(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 401 | """Integers are their own numerators.""" |
Guido van Rossum | d425630 | 2007-12-06 17:45:33 +0000 | [diff] [blame] | 402 | return +self |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 403 | |
| 404 | @property |
| 405 | def denominator(self): |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 406 | """Integers have a denominator of 1.""" |
Guido van Rossum | 1daf954 | 2007-08-30 17:45:54 +0000 | [diff] [blame] | 407 | return 1 |
| 408 | |
| 409 | Integral.register(int) |