int.as_integer_ratio()

Trivial on its own — it exists so int and float share an interface, letting one code path handle both without type checks.

Int methodPython 3.8+Live demo
Common call
n.as_integer_ratio()
Returns
(n, 1) — always, for every int
Replaces
isinstance checks before calling the float version
Watch out
the point is the shared interface, not the value; on int alone it tells you nothing new
int.as_integer_ratio()
→ tuple

Demo

Live evaluation
Try:
Inputs
nintinteger
Output
(5).as_integer_ratio()
(5, 1)

For an int the answer is always (n, 1) — an integer is already a whole number over one. That looks pointless until you remember float has the same method and returns a genuine fraction: (0.5).as_integer_ratio() is (1, 2). Because both types answer the same question, numeric code can call it on either without an isinstance check. The sign stays on the numerator; the denominator is always positive.

Common patterns

Type-agnostic exact arithmetic
Works for int and float alike — no branching on the type.
num, den = value.as_integer_ratio()
exact = Fraction(num, den)
Sum a mix of ints and floats exactly
Avoids float rounding by staying in integer arithmetic throughout.
from fractions import Fraction
total = sum(Fraction(*v.as_integer_ratio()) for v in values)
Compare a float to an int without loss
Cross-multiplying the ratios is exact where float comparison is not.
an, ad = a.as_integer_ratio()
bn, bd = b.as_integer_ratio()
equal = an * bd == bn * ad

Examples

1. Always over one
(5).as_integer_ratio()
Returns
(5, 1)
2. Zero
(0).as_integer_ratio()
Returns
(0, 1)
3. Sign on numerator
(-7).as_integer_ratio()
Returns
(-7, 1)
4. Large values fine
(123456).as_integer_ratio()
Returns
(123456, 1)
5. Contrast with float
(0.5).as_integer_ratio()
Returns
(1, 2)
6. Float surprise
(0.1).as_integer_ratio()
Returns
(3602879701896397, 36028797018963968)

Pitfalls

1. On int alone it looks useless
Reading it in isolation, (5, 1) seems like a wasted call. The value is entirely in the shared interface with float — writing it off means reaching for isinstance branches you did not need.
Needless branching
if isinstance(v, int):
    num, den = v, 1
else:
    num, den = v.as_integer_ratio()
two paths doing one job
One path
num, den = v.as_integer_ratio()
works for int and float
2. The float version is exact, not tidy
Because binary floats cannot hold 0.1 exactly, the ratio you get back is the huge fraction the float actually stores. That is correct, and usually surprising.
Expected (1, 10)
(0.1).as_integer_ratio()
(3602879701896397, 36028797018963968)
Start from a string
from fractions import Fraction
Fraction('0.1')
Fraction(1, 10)
3. Not available before Python 3.8
float has had it for much longer, but int only gained it in 3.8. Code relying on the shared interface breaks on 3.7 with an AttributeError from the int side.
Fails on 3.7
(5).as_integer_ratio()
AttributeError: 'int' object has no attribute 'as_integer_ratio'
Fallback
num, den = (v.as_integer_ratio() if hasattr(v, "as_integer_ratio") else (v, 1))
portable

When to use

Use it
  • Numeric code that must accept int and float interchangeably
  • Converting any real number to fractions.Fraction without type checks
  • Exact comparison or summation where float rounding is unacceptable
Reach for something else
  • You already know the value is an int — the answer is (n, 1)
  • You want a readable fraction from a decimal → Fraction(str) or decimal.Decimal
  • Supporting Python 3.7 or older without a fallback

Notes

Complexity
O(1) — builds a two-item tuple
Return
A new tuple (n, 1); the denominator is always exactly 1
CPython impl
Objects/longobject.c :: int_as_integer_ratio
Memory
Allocates one small tuple
Thread-safe
Yes — ints and tuples are immutable

FAQ

You would not, if you knew it was an int. It exists so that code accepting "any real number" can call one method on either type. int gained it in 3.8 specifically to close that gap with float.

num, den = value.as_integer_ratio()   # value may be int or float

History

3.8
int.as_integer_ratio added, matching the float method of the same name.