LEFT | RIGHT |
1 # Copyright 2007 Google, Inc. All Rights Reserved. | 1 # Copyright 2007 Google, Inc. All Rights Reserved. |
2 # Licensed to PSF under a Contributor Agreement. | 2 # Licensed to PSF under a Contributor Agreement. |
3 | 3 |
4 """Abstract Base Classes (ABCs) for numbers, according to PEP 3141. | 4 """Abstract Base Classes (ABCs) for numbers, according to PEP 3141. |
5 | 5 |
6 TODO: Fill out more detailed documentation on the operators.""" | 6 TODO: Fill out more detailed documentation on the operators.""" |
7 | 7 |
8 from abc import ABCMeta, abstractmethod | 8 from abc import ABCMeta, abstractmethod |
9 | 9 |
10 __all__ = ["Number", "Complex", "Real", "Rational", "Integral"] | 10 __all__ = ["Number", "Complex", "Real", "Rational", "Integral"] |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 """Integral adds a conversion to int and the bit-string operations.""" | 300 """Integral adds a conversion to int and the bit-string operations.""" |
301 | 301 |
302 __slots__ = () | 302 __slots__ = () |
303 | 303 |
304 @abstractmethod | 304 @abstractmethod |
305 def __int__(self): | 305 def __int__(self): |
306 """int(self)""" | 306 """int(self)""" |
307 raise NotImplementedError | 307 raise NotImplementedError |
308 | 308 |
309 def __index__(self): | 309 def __index__(self): |
310 """someobject[self]""" | 310 """Called whenever an index is needed, such as in slicing""" |
311 return int(self) | 311 return int(self) |
312 | 312 |
313 @abstractmethod | 313 @abstractmethod |
314 def __pow__(self, exponent, modulus=None): | 314 def __pow__(self, exponent, modulus=None): |
315 """self ** exponent % modulus, but maybe faster. | 315 """self ** exponent % modulus, but maybe faster. |
316 | 316 |
317 Accept the modulus argument if you want to support the | 317 Accept the modulus argument if you want to support the |
318 3-argument version of pow(). Raise a TypeError if exponent < 0 | 318 3-argument version of pow(). Raise a TypeError if exponent < 0 |
319 or any argument isn't Integral. Otherwise, just implement the | 319 or any argument isn't Integral. Otherwise, just implement the |
320 2-argument version described in Complex. | 320 2-argument version described in Complex. |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 def numerator(self): | 385 def numerator(self): |
386 """Integers are their own numerators.""" | 386 """Integers are their own numerators.""" |
387 return +self | 387 return +self |
388 | 388 |
389 @property | 389 @property |
390 def denominator(self): | 390 def denominator(self): |
391 """Integers have a denominator of 1.""" | 391 """Integers have a denominator of 1.""" |
392 return 1 | 392 return 1 |
393 | 393 |
394 Integral.register(int) | 394 Integral.register(int) |
LEFT | RIGHT |