Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(161766)

Side by Side Diff: Lib/numbers.py

Issue 11610: Improving property to accept abstract methods
Patch Set: Created 8 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/abc.py ('k') | Lib/test/test_abc.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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, abstractproperty 8 from abc import ABCMeta, abstractmethod
9 9
10 __all__ = ["Number", "Complex", "Real", "Rational", "Integral"] 10 __all__ = ["Number", "Complex", "Real", "Rational", "Integral"]
11 11
12 class Number(metaclass=ABCMeta): 12 class Number(metaclass=ABCMeta):
13 """All numbers inherit from this class. 13 """All numbers inherit from this class.
14 14
15 If you just want to check if an argument x is a number, without 15 If you just want to check if an argument x is a number, without
16 caring what kind, use isinstance(x, Number). 16 caring what kind, use isinstance(x, Number).
17 """ 17 """
18 __slots__ = () 18 __slots__ = ()
(...skipping 24 matching lines...) Expand all
43 __slots__ = () 43 __slots__ = ()
44 44
45 @abstractmethod 45 @abstractmethod
46 def __complex__(self): 46 def __complex__(self):
47 """Return a builtin complex instance. Called for complex(self).""" 47 """Return a builtin complex instance. Called for complex(self)."""
48 48
49 def __bool__(self): 49 def __bool__(self):
50 """True if self != 0. Called for bool(self).""" 50 """True if self != 0. Called for bool(self)."""
51 return self != 0 51 return self != 0
52 52
53 @abstractproperty 53 @property
54 @abstractmethod
54 def real(self): 55 def real(self):
55 """Retrieve the real component of this number. 56 """Retrieve the real component of this number.
56 57
57 This should subclass Real. 58 This should subclass Real.
58 """ 59 """
59 raise NotImplementedError 60 raise NotImplementedError
60 61
61 @abstractproperty 62 @property
63 @abstractmethod
62 def imag(self): 64 def imag(self):
63 """Retrieve the imaginary component of this number. 65 """Retrieve the imaginary component of this number.
64 66
65 This should subclass Real. 67 This should subclass Real.
66 """ 68 """
67 raise NotImplementedError 69 raise NotImplementedError
68 70
69 @abstractmethod 71 @abstractmethod
70 def __add__(self, other): 72 def __add__(self, other):
71 """self + other""" 73 """self + other"""
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 return +self 267 return +self
266 268
267 Real.register(float) 269 Real.register(float)
268 270
269 271
270 class Rational(Real): 272 class Rational(Real):
271 """.numerator and .denominator should be in lowest terms.""" 273 """.numerator and .denominator should be in lowest terms."""
272 274
273 __slots__ = () 275 __slots__ = ()
274 276
275 @abstractproperty 277 @property
278 @abstractmethod
276 def numerator(self): 279 def numerator(self):
277 raise NotImplementedError 280 raise NotImplementedError
278 281
279 @abstractproperty 282 @property
283 @abstractmethod
280 def denominator(self): 284 def denominator(self):
281 raise NotImplementedError 285 raise NotImplementedError
282 286
283 # Concrete implementation of Real's conversion to float. 287 # Concrete implementation of Real's conversion to float.
284 def __float__(self): 288 def __float__(self):
285 """float(self) = self.numerator / self.denominator 289 """float(self) = self.numerator / self.denominator
286 290
287 It's important that this conversion use the integer's "true" 291 It's important that this conversion use the integer's "true"
288 division rather than casting one side to float before dividing 292 division rather than casting one side to float before dividing
289 so that ratios of huge integers convert without overflowing. 293 so that ratios of huge integers convert without overflowing.
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 def numerator(self): 385 def numerator(self):
382 """Integers are their own numerators.""" 386 """Integers are their own numerators."""
383 return +self 387 return +self
384 388
385 @property 389 @property
386 def denominator(self): 390 def denominator(self):
387 """Integers have a denominator of 1.""" 391 """Integers have a denominator of 1."""
388 return 1 392 return 1
389 393
390 Integral.register(int) 394 Integral.register(int)
OLDNEW
« no previous file with comments | « Lib/abc.py ('k') | Lib/test/test_abc.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+