On this page
numbers
Prev: nturl2path | Up: stdlib_module | Next: opcode
Rel: math, cmath, decimal, fractions, random
Ref: docs.python.org — numbers
Tags: #public #python3 #stdlib
Import
import numbers
Overview
Abstract numeric tower (Number > Complex > Real > Rational > Integral) defining mathematical interfaces rather than concrete types. Use isinstance(x, numbers.Real) or numbers.Integral when writing generic math utilities that should accept built-ins, Fraction, Decimal (note caveat below), or NumPy numeric scalars.
Quick Start
import numbers
from fractions import Fraction
assert isinstance(42, numbers.Integral)
assert isinstance(Fraction(1, 3), numbers.Rational)
assert isinstance(3.14, numbers.Real)
assert isinstance(1 + 2j, numbers.Complex)
assert not isinstance(1 + 2j, numbers.Real)
Implementation Walkthrough
1. Writing Numeric Helpers That Reject Booleans Cleanly
Because bool is a subclass of int, isinstance(True, numbers.Real) is True—exclude bool explicitly when validating numeric inputs:
import numbers
def ensure_real(value: object) -> float:
if isinstance(value, bool) or not isinstance(value, numbers.Real):
raise TypeError(f"Expected real number, got {type(value).__name__}")
return float(value)
assert ensure_real(3) == 3.0
Gotchas & Boundaries
decimal.Decimalis NOT a subclass ofnumbers.Real: BecauseDecimaldoesn't interoperate seamlessly with binaryfloat, it only registers asnumbers.Number—check(numbers.Real, Decimal)if you accept both.isinstance(True, numbers.Integral)isTrue: Always guard withnot isinstance(x, bool)when booleans shouldn't count as numbers.- For static type checkers (
mypy/pyright), preferint | floatovernumbers.Realbecause the numeric ABCs aren't fully compatible with static numeric protocols.
Classes & Objects
- numbers.Complex — Complex defines the operations that work on the builtin complex type. (Methods:
.conjugate(),.imag(),.real()) - numbers.Integral — Integral adds methods that work on integral numbers. (Methods:
.conjugate(),.denominator(),.imag(),.numerator(),.real()) - numbers.Number — All numbers inherit from this class.
- numbers.Rational — To Real, Rational adds numerator and denominator properties. (Methods:
.conjugate(),.denominator(),.imag(),.numerator(),.real()) - numbers.Real — To Complex, Real adds the operations that work on real numbers. (Methods:
.conjugate(),.imag(),.real())
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/cmath
- tuts/python3/stdlib_module/decimal
- tuts/python3/stdlib_module/fractions
- tuts/python3/stdlib_module/math
- tuts/python3/stdlib_module/nturl2path
- tuts/python3/stdlib_module/numbers/Complex
- tuts/python3/stdlib_module/numbers/Integral
- tuts/python3/stdlib_module/numbers/Number
- tuts/python3/stdlib_module/numbers/Rational
- tuts/python3/stdlib_module/numbers/Real
- tuts/python3/stdlib_module/opcode
- tuts/python3/stdlib_module/random
- tuts/python3/stdlib_module/statistics
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/cmath Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/decimal Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/fractions Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/math Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/nturl2path Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/numbers/Complex Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/numbers/Integral Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/numbers/Number Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/numbers/Rational Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/numbers/Real Linked here; links to this page; shared tags: #stdlib