summaryrefslogtreecommitdiff
path: root/devdocs/python~3.12/library%2Fnumbers.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/python~3.12/library%2Fnumbers.html')
-rw-r--r--devdocs/python~3.12/library%2Fnumbers.html112
1 files changed, 112 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Fnumbers.html b/devdocs/python~3.12/library%2Fnumbers.html
new file mode 100644
index 00000000..6775e63d
--- /dev/null
+++ b/devdocs/python~3.12/library%2Fnumbers.html
@@ -0,0 +1,112 @@
+ <span id="numbers-numeric-abstract-base-classes"></span><h1>numbers — Numeric abstract base classes</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/numbers.py">Lib/numbers.py</a></p> <p>The <code>numbers</code> module (<span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-3141/"><strong>PEP 3141</strong></a>) defines a hierarchy of numeric <a class="reference internal" href="../glossary#term-abstract-base-class"><span class="xref std std-term">abstract base classes</span></a> which progressively define more operations. None of the types defined in this module are intended to be instantiated.</p> <dl class="py class"> <dt class="sig sig-object py" id="numbers.Number">
+<code>class numbers.Number</code> </dt> <dd>
+<p>The root of the numeric hierarchy. If you just want to check if an argument <em>x</em> is a number, without caring what kind, use <code>isinstance(x, Number)</code>.</p> </dd>
+</dl> <section id="the-numeric-tower"> <h2>The numeric tower</h2> <dl class="py class"> <dt class="sig sig-object py" id="numbers.Complex">
+<code>class numbers.Complex</code> </dt> <dd>
+<p>Subclasses of this type describe complex numbers and include the operations that work on the built-in <a class="reference internal" href="functions#complex" title="complex"><code>complex</code></a> type. These are: conversions to <a class="reference internal" href="functions#complex" title="complex"><code>complex</code></a> and <a class="reference internal" href="functions#bool" title="bool"><code>bool</code></a>, <a class="reference internal" href="#numbers.Complex.real" title="numbers.Complex.real"><code>real</code></a>, <a class="reference internal" href="#numbers.Complex.imag" title="numbers.Complex.imag"><code>imag</code></a>, <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, <code>**</code>, <a class="reference internal" href="functions#abs" title="abs"><code>abs()</code></a>, <a class="reference internal" href="#numbers.Complex.conjugate" title="numbers.Complex.conjugate"><code>conjugate()</code></a>, <code>==</code>, and <code>!=</code>. All except <code>-</code> and <code>!=</code> are abstract.</p> <dl class="py attribute"> <dt class="sig sig-object py" id="numbers.Complex.real">
+<code>real</code> </dt> <dd>
+<p>Abstract. Retrieves the real component of this number.</p> </dd>
+</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="numbers.Complex.imag">
+<code>imag</code> </dt> <dd>
+<p>Abstract. Retrieves the imaginary component of this number.</p> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="numbers.Complex.conjugate">
+<code>abstractmethod conjugate()</code> </dt> <dd>
+<p>Abstract. Returns the complex conjugate. For example, <code>(1+3j).conjugate()
+== (1-3j)</code>.</p> </dd>
+</dl> </dd>
+</dl> <dl class="py class"> <dt class="sig sig-object py" id="numbers.Real">
+<code>class numbers.Real</code> </dt> <dd>
+<p>To <a class="reference internal" href="#numbers.Complex" title="numbers.Complex"><code>Complex</code></a>, <code>Real</code> adds the operations that work on real numbers.</p> <p>In short, those are: a conversion to <a class="reference internal" href="functions#float" title="float"><code>float</code></a>, <a class="reference internal" href="math#math.trunc" title="math.trunc"><code>math.trunc()</code></a>, <a class="reference internal" href="functions#round" title="round"><code>round()</code></a>, <a class="reference internal" href="math#math.floor" title="math.floor"><code>math.floor()</code></a>, <a class="reference internal" href="math#math.ceil" title="math.ceil"><code>math.ceil()</code></a>, <a class="reference internal" href="functions#divmod" title="divmod"><code>divmod()</code></a>, <code>//</code>, <code>%</code>, <code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, and <code>&gt;=</code>.</p> <p>Real also provides defaults for <a class="reference internal" href="functions#complex" title="complex"><code>complex()</code></a>, <a class="reference internal" href="#numbers.Complex.real" title="numbers.Complex.real"><code>real</code></a>, <a class="reference internal" href="#numbers.Complex.imag" title="numbers.Complex.imag"><code>imag</code></a>, and <a class="reference internal" href="#numbers.Complex.conjugate" title="numbers.Complex.conjugate"><code>conjugate()</code></a>.</p> </dd>
+</dl> <dl class="py class"> <dt class="sig sig-object py" id="numbers.Rational">
+<code>class numbers.Rational</code> </dt> <dd>
+<p>Subtypes <a class="reference internal" href="#numbers.Real" title="numbers.Real"><code>Real</code></a> and adds <a class="reference internal" href="#numbers.Rational.numerator" title="numbers.Rational.numerator"><code>numerator</code></a> and <a class="reference internal" href="#numbers.Rational.denominator" title="numbers.Rational.denominator"><code>denominator</code></a> properties. It also provides a default for <a class="reference internal" href="functions#float" title="float"><code>float()</code></a>.</p> <p>The <a class="reference internal" href="#numbers.Rational.numerator" title="numbers.Rational.numerator"><code>numerator</code></a> and <a class="reference internal" href="#numbers.Rational.denominator" title="numbers.Rational.denominator"><code>denominator</code></a> values should be instances of <a class="reference internal" href="#numbers.Integral" title="numbers.Integral"><code>Integral</code></a> and should be in lowest terms with <a class="reference internal" href="#numbers.Rational.denominator" title="numbers.Rational.denominator"><code>denominator</code></a> positive.</p> <dl class="py attribute"> <dt class="sig sig-object py" id="numbers.Rational.numerator">
+<code>numerator</code> </dt> <dd>
+<p>Abstract.</p> </dd>
+</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="numbers.Rational.denominator">
+<code>denominator</code> </dt> <dd>
+<p>Abstract.</p> </dd>
+</dl> </dd>
+</dl> <dl class="py class"> <dt class="sig sig-object py" id="numbers.Integral">
+<code>class numbers.Integral</code> </dt> <dd>
+<p>Subtypes <a class="reference internal" href="#numbers.Rational" title="numbers.Rational"><code>Rational</code></a> and adds a conversion to <a class="reference internal" href="functions#int" title="int"><code>int</code></a>. Provides defaults for <a class="reference internal" href="functions#float" title="float"><code>float()</code></a>, <a class="reference internal" href="#numbers.Rational.numerator" title="numbers.Rational.numerator"><code>numerator</code></a>, and <a class="reference internal" href="#numbers.Rational.denominator" title="numbers.Rational.denominator"><code>denominator</code></a>. Adds abstract methods for <a class="reference internal" href="functions#pow" title="pow"><code>pow()</code></a> with modulus and bit-string operations: <code>&lt;&lt;</code>, <code>&gt;&gt;</code>, <code>&amp;</code>, <code>^</code>, <code>|</code>, <code>~</code>.</p> </dd>
+</dl> </section> <section id="notes-for-type-implementors"> <h2>Notes for type implementors</h2> <p>Implementors should be careful to make equal numbers equal and hash them to the same values. This may be subtle if there are two different extensions of the real numbers. For example, <a class="reference internal" href="fractions#fractions.Fraction" title="fractions.Fraction"><code>fractions.Fraction</code></a> implements <a class="reference internal" href="functions#hash" title="hash"><code>hash()</code></a> as follows:</p> <pre data-language="python">def __hash__(self):
+ if self.denominator == 1:
+ # Get integers right.
+ return hash(self.numerator)
+ # Expensive check, but definitely correct.
+ if self == float(self):
+ return hash(float(self))
+ else:
+ # Use tuple's hash to avoid a high collision rate on
+ # simple fractions.
+ return hash((self.numerator, self.denominator))
+</pre> <section id="adding-more-numeric-abcs"> <h3>Adding More Numeric ABCs</h3> <p>There are, of course, more possible ABCs for numbers, and this would be a poor hierarchy if it precluded the possibility of adding those. You can add <code>MyFoo</code> between <a class="reference internal" href="#numbers.Complex" title="numbers.Complex"><code>Complex</code></a> and <a class="reference internal" href="#numbers.Real" title="numbers.Real"><code>Real</code></a> with:</p> <pre data-language="python">class MyFoo(Complex): ...
+MyFoo.register(Real)
+</pre> </section> <section id="implementing-the-arithmetic-operations"> <span id="id1"></span><h3>Implementing the arithmetic operations</h3> <p>We want to implement the arithmetic operations so that mixed-mode operations either call an implementation whose author knew about the types of both arguments, or convert both to the nearest built in type and do the operation there. For subtypes of <a class="reference internal" href="#numbers.Integral" title="numbers.Integral"><code>Integral</code></a>, this means that <a class="reference internal" href="../reference/datamodel#object.__add__" title="object.__add__"><code>__add__()</code></a> and <a class="reference internal" href="../reference/datamodel#object.__radd__" title="object.__radd__"><code>__radd__()</code></a> should be defined as:</p> <pre data-language="python">class MyIntegral(Integral):
+
+ def __add__(self, other):
+ if isinstance(other, MyIntegral):
+ return do_my_adding_stuff(self, other)
+ elif isinstance(other, OtherTypeIKnowAbout):
+ return do_my_other_adding_stuff(self, other)
+ else:
+ return NotImplemented
+
+ def __radd__(self, other):
+ if isinstance(other, MyIntegral):
+ return do_my_adding_stuff(other, self)
+ elif isinstance(other, OtherTypeIKnowAbout):
+ return do_my_other_adding_stuff(other, self)
+ elif isinstance(other, Integral):
+ return int(other) + int(self)
+ elif isinstance(other, Real):
+ return float(other) + float(self)
+ elif isinstance(other, Complex):
+ return complex(other) + complex(self)
+ else:
+ return NotImplemented
+</pre> <p>There are 5 different cases for a mixed-type operation on subclasses of <a class="reference internal" href="#numbers.Complex" title="numbers.Complex"><code>Complex</code></a>. I’ll refer to all of the above code that doesn’t refer to <code>MyIntegral</code> and <code>OtherTypeIKnowAbout</code> as “boilerplate”. <code>a</code> will be an instance of <code>A</code>, which is a subtype of <a class="reference internal" href="#numbers.Complex" title="numbers.Complex"><code>Complex</code></a> (<code>a : A &lt;: Complex</code>), and <code>b : B &lt;:
+Complex</code>. I’ll consider <code>a + b</code>:</p> <ol class="arabic simple"> <li>If <code>A</code> defines an <a class="reference internal" href="../reference/datamodel#object.__add__" title="object.__add__"><code>__add__()</code></a> which accepts <code>b</code>, all is well.</li> <li>If <code>A</code> falls back to the boilerplate code, and it were to return a value from <a class="reference internal" href="../reference/datamodel#object.__add__" title="object.__add__"><code>__add__()</code></a>, we’d miss the possibility that <code>B</code> defines a more intelligent <a class="reference internal" href="../reference/datamodel#object.__radd__" title="object.__radd__"><code>__radd__()</code></a>, so the boilerplate should return <a class="reference internal" href="constants#NotImplemented" title="NotImplemented"><code>NotImplemented</code></a> from <code>__add__()</code>. (Or <code>A</code> may not implement <code>__add__()</code> at all.)</li> <li>Then <code>B</code>’s <a class="reference internal" href="../reference/datamodel#object.__radd__" title="object.__radd__"><code>__radd__()</code></a> gets a chance. If it accepts <code>a</code>, all is well.</li> <li>If it falls back to the boilerplate, there are no more possible methods to try, so this is where the default implementation should live.</li> <li>If <code>B &lt;: A</code>, Python tries <code>B.__radd__</code> before <code>A.__add__</code>. This is ok, because it was implemented with knowledge of <code>A</code>, so it can handle those instances before delegating to <a class="reference internal" href="#numbers.Complex" title="numbers.Complex"><code>Complex</code></a>.</li> </ol> <p>If <code>A &lt;: Complex</code> and <code>B &lt;: Real</code> without sharing any other knowledge, then the appropriate shared operation is the one involving the built in <a class="reference internal" href="functions#complex" title="complex"><code>complex</code></a>, and both <a class="reference internal" href="../reference/datamodel#object.__radd__" title="object.__radd__"><code>__radd__()</code></a> s land there, so <code>a+b
+== b+a</code>.</p> <p>Because most of the operations on any given type will be very similar, it can be useful to define a helper function which generates the forward and reverse instances of any given operator. For example, <a class="reference internal" href="fractions#fractions.Fraction" title="fractions.Fraction"><code>fractions.Fraction</code></a> uses:</p> <pre data-language="python">def _operator_fallbacks(monomorphic_operator, fallback_operator):
+ def forward(a, b):
+ if isinstance(b, (int, Fraction)):
+ return monomorphic_operator(a, b)
+ elif isinstance(b, float):
+ return fallback_operator(float(a), b)
+ elif isinstance(b, complex):
+ return fallback_operator(complex(a), b)
+ else:
+ return NotImplemented
+ forward.__name__ = '__' + fallback_operator.__name__ + '__'
+ forward.__doc__ = monomorphic_operator.__doc__
+
+ def reverse(b, a):
+ if isinstance(a, Rational):
+ # Includes ints.
+ return monomorphic_operator(a, b)
+ elif isinstance(a, Real):
+ return fallback_operator(float(a), float(b))
+ elif isinstance(a, Complex):
+ return fallback_operator(complex(a), complex(b))
+ else:
+ return NotImplemented
+ reverse.__name__ = '__r' + fallback_operator.__name__ + '__'
+ reverse.__doc__ = monomorphic_operator.__doc__
+
+ return forward, reverse
+
+def _add(a, b):
+ """a + b"""
+ return Fraction(a.numerator * b.denominator +
+ b.numerator * a.denominator,
+ a.denominator * b.denominator)
+
+__add__, __radd__ = _operator_fallbacks(_add, operator.add)
+
+# ...
+</pre> </section> </section> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; 2001&ndash;2023 Python Software Foundation<br>Licensed under the PSF License.<br>
+ <a href="https://docs.python.org/3.12/library/numbers.html" class="_attribution-link">https://docs.python.org/3.12/library/numbers.html</a>
+ </p>
+</div>