Python 3
sys.maxint
integer limits
Python sys module
programming

What is sys.maxint in Python 3?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

sys.maxint existed in Python 2 as the maximum value of the platform int type. In Python 3, sys.maxint was removed because integer behavior changed fundamentally: int now has arbitrary precision and no fixed upper bound (limited only by memory).

Many migration issues come from old code checking sys.maxint for bounds or sentinel values. The modern replacement depends on intent. If you need pointer-sized limits, use sys.maxsize. If you need large-number safety, Python 3 integers already scale.

Core Sections

1. Python 2 vs Python 3 integer model

Python 2 had two integer types:

  • int (fixed-size, machine-dependent bound)
  • long (arbitrary precision)

Python 3 unified these into one int type.

python
1# Python 3
2x = 10 ** 200
3print(type(x))  # <class 'int'>
4print(len(str(x)))  # 201 digits

Because integers expand automatically, sys.maxint no longer makes sense in Python 3.

2. What to use instead of sys.maxint

If old code used sys.maxint as "largest practical index" or for C-like bounds, use sys.maxsize.

python
1import sys
2
3print(sys.maxsize)
4# e.g., 9223372036854775807 on 64-bit platforms

sys.maxsize is primarily the largest supported container size / Py_ssize_t value, not the maximum int value.

Migration example:

python
1# Old Python 2 style
2# sentinel = sys.maxint
3
4# Python 3
5import sys
6sentinel = sys.maxsize

Choose this only when your logic is about platform-sized limits.

3. Practical implications for modern Python code

Since int is unbounded, overflow behavior differs from languages with fixed-width integers.

python
a = 2 ** 63 - 1
b = a + 1
print(b)  # still valid int

But external systems may still have fixed limits (databases, protocols, JSON consumers, C extensions). Validate before crossing boundaries.

python
1def to_int32(n: int) -> int:
2    if not (-2**31 <= n < 2**31):
3        raise ValueError("out of int32 range")
4    return n

For serialization, especially to JavaScript clients, very large integers may lose precision unless encoded as strings.

Common Pitfalls

  • Searching for sys.maxint in Python 3 and assuming it was renamed one-to-one.
  • Using sys.maxsize as if it were the maximum Python integer value.
  • Ignoring downstream fixed-width limits when Python integers exceed external bounds.
  • Porting Python 2 code without revisiting assumptions around integer overflow.
  • Sending huge integers to JSON/JS clients without precision safeguards.

Summary

sys.maxint does not exist in Python 3 because Python integers are arbitrary precision. Use sys.maxsize only when you need platform-sized limits related to container/index semantics. For general arithmetic, Python 3 int already handles large values, but you still need boundary checks when integrating with fixed-width external systems.

During Python 2 to 3 migration, add static checks that flag sys.maxint references and auto-suggest replacements. Many such usages are actually sentinel choices that should be replaced with domain-specific constants instead of sys.maxsize. Reviewing each call site improves correctness and often clarifies business logic that previously relied on implicit integer assumptions.

If your Python code interfaces with C/C++ extensions, document conversion boundaries explicitly. Python can represent huge integers, but extension APIs may truncate or reject values depending on expected C types. Runtime boundary validation and clear exceptions are better than silent overflow behavior.

Migration guides should explain not just symbol replacement but the conceptual shift to arbitrary-precision integers so teams avoid repeating old assumptions.

When reviewing old code, replace magic "max int" sentinels with domain constants (MAX_RETRY, MAX_PAGE_SIZE) where possible. This makes intent clearer and avoids accidental coupling to platform-size assumptions. As a result, the migrated code is easier to read and less error-prone than a direct mechanical substitution.

Prefer clear domain limits over generic "largest possible" sentinels.

That clarity improves migration quality.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.