Featureadvanced
The numeric tower
Also called numbers module, numeric abstract base classes, numbers.Number, numbers.Real, numbers.Integral, PEP 3141.
The numbers module defines the abstract base classes Number, Complex, Real, Rational and Integral. Each one adds operations to the one above it, and isinstance() with them tests what a number can do.
- Language
- Python
Synopsis
from decimal import Decimal
from fractions import Fraction
from numbers import Complex, Integral, Number, Rational, Real
abcs = (Number, Complex, Real, Rational, Integral)
for v in [7, True, 1.5, 2j, Fraction(1, 3), Decimal("1.1")]:
print(type(v).__name__, [c.__name__ for c in abcs if isinstance(v, c)])
int ['Number', 'Complex', 'Real', 'Rational', 'Integral']
bool ['Number', 'Complex', 'Real', 'Rational', 'Integral']
float ['Number', 'Complex', 'Real']
complex ['Number', 'Complex']
Fraction ['Number', 'Complex', 'Real', 'Rational']
Decimal ['Number']
Each built-in number type is an instance of the classes above its own level. The output shows that Decimal is a Number but not a Real.
Semantics
r1. The numbers module "defines a hierarchy of numeric abstract base classes which progressively define more operations". None of its types is intended to be instantiated.1 Example: no-instances.
r2. Number is the root of the hierarchy. To check that x is a number of any kind, use isinstance(x, Number).1 Example: tower-membership.
r3. Complex has the operations of the built-in complex type: conversions to complex and bool, real, imag, +, -, *, /, **, abs(), conjugate(), == and !=. Real adds the operations on real numbers: conversion to float, math.trunc(), round(), math.floor(), math.ceil(), divmod(), //, % and the order comparisons.1
r4. Rational adds the numerator and denominator properties, which should be in lowest terms with a positive denominator. Integral adds conversion to int, pow() with a modulus, and the bit-string operations <<, >>, &, ^, | and ~.1 Example: fractions-of-numbers.
r5. int implements numbers.Integral, float implements numbers.Real, and complex implements numbers.Complex.2 fractions.Fraction inherits from numbers.Rational.3 Example: tower-membership.
r6. More classes can be added to the hierarchy. For example, a class MyFoo that subclasses Complex is put between Complex and Real with MyFoo.register(Real).1 Example: insert-a-level.
r7. Implementers should "make equal numbers equal and hash them to the same values". A mixed-type operation should either call an implementation that knows both types, or convert both operands to the nearest built-in type. So __add__() returns NotImplemented for an operand that it does not know, and __radd__() holds the fallback to int, float or complex.1 Example: mixed-mode.
Examples
Every Rational has a numerator and a denominator, so an int has them too, and every Real has real and imag (rules r3 and r4).
from fractions import Fraction
print(Fraction(6, 8).numerator, Fraction(6, 8).denominator)
print((7).numerator, (7).denominator, (2.5).real, (2.5).imag)
3 4
7 1 2.5 0.0
MyFoo.register(Real) makes Real, and so float and int, virtual subclasses of MyFoo. complex is not one (rule r6).
from numbers import Complex, Real
class MyFoo(Complex):
pass
MyFoo.register(Real)
print(issubclass(Real, MyFoo), isinstance(1.5, MyFoo), isinstance(7, MyFoo))
print(isinstance(1j, MyFoo))
True True True
False
Tally.__add__() knows only Tally, and Tally.__radd__() falls back to the built-in types (rule r7). For 1 + Tally(2), int.__add__() gives NotImplemented, and Python calls Tally.__radd__().
from numbers import Integral, Real
class Tally:
def __init__(self, n):
self.n = n
def __add__(self, other):
if isinstance(other, Tally):
return Tally(self.n + other.n)
return NotImplemented
def __radd__(self, other):
if isinstance(other, Integral):
return int(other) + self.n
if isinstance(other, Real):
return float(other) + self.n
return NotImplemented
print((Tally(2) + Tally(3)).n, 1 + Tally(2), 1.5 + Tally(2))
5 3 3.5
Pitfalls
isinstance(x, float) to test for a real number
An int or a Fraction is a real number, but it is not a float. Test with numbers.Real (rule r5).
This code runs but gives a wrong answer on purpose.
from fractions import Fraction
from numbers import Real
for v in [2, 2.0, Fraction(1, 2)]:
print(isinstance(v, float), isinstance(v, Real))
False True
True True
False True
Instantiating an abstract class
The classes of the tower are abstract (rule r1). numbers.Real() raises TypeError, because Real has abstract methods.
This code raises TypeError, on purpose.
import numbers
print(numbers.Real())
bool is Integral
True passes an isinstance(x, Integral) test (see tower-membership). Exclude bool explicitly when a function must reject it.
Comparison
Go has no hierarchy of number types. For a binary operator other than a shift or a comparison, "the operand types must be identical" unless an untyped constant is involved.4 So an int and a float64 cannot be added without a conversion. Python's tower and the NotImplemented protocol (rule r7) let operands of different number types mix.
References
- numbers, Numeric abstract base classes, Python 3.15 documentation, The numeric tower; Notes for type implementers
- Built-in Types, Python 3.15 documentation, Additional Methods on Integer Types, on Float, on Complex
- fractions, Rational numbers, Python 3.15 documentation, class fractions.Fraction
- The Go Programming Language Specification, Operators
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag GYWPG8 Edit this page History Report an error Markdown