Skip to content

Commit d8258db

Browse files
authored
Update README.md
Add new feature: Exceeds the limit for integer string conversion
1 parent 6a4cd34 commit d8258db

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ So, here we go...
5959
+ [▶ The disappearing variable from outer scope](#-the-disappearing-variable-from-outer-scope)
6060
+ [▶ The mysterious key type conversion](#-the-mysterious-key-type-conversion)
6161
+ [▶ Let's see if you can guess this?](#-lets-see-if-you-can-guess-this)
62+
+ [▶ Exceeds the limit for integer string conversion](#-exceeds-the-limit-for-integer-string-conversion)
6263
* [Section: Slippery Slopes](#section-slippery-slopes)
6364
+ [▶ Modifying a dictionary while iterating over it](#-modifying-a-dictionary-while-iterating-over-it)
6465
+ [▶ Stubborn `del` operation](#-stubborn-del-operation)
@@ -1975,9 +1976,45 @@ a, b = a[b] = {}, 5
19751976
True
19761977
```
19771978
1979+
19781980
---
1981+
1982+
### ▶ Exceeds the limit for integer string conversion
1983+
```py
1984+
>>> # Python 3.10.6
1985+
>>> int("2" * 5432)
1986+
1987+
>>> # Python 3.10.8
1988+
>>> int("2" * 5432)
1989+
```
1990+
1991+
**Output:**
1992+
```py
1993+
>>> # Python 3.10.6
1994+
222222222222222222222222222222222222222222222222222222222222222...
1995+
1996+
>>> # Python 3.10.8
1997+
Traceback (most recent call last):
1998+
...
1999+
ValueError: Exceeds the limit (4300) for integer string conversion:
2000+
value has 5432 digits; use sys.set_int_max_str_digits()
2001+
to increase the limit.
2002+
```
2003+
2004+
#### 💡 Explanation:
2005+
This call to `int()` works fine in Python 3.10.6 and raises a ValueError in Python 3.10.8. Note that Python can still work with large integers. The error is only raised when converting between integers and strings.
2006+
2007+
Fortunately, you can increase the limit for the allowed number of digits when you expect an operation to exceed it. To do this, you can use one of the following:
2008+
- The -X int_max_str_digits command-line flag
2009+
- The set_int_max_str_digits() function from the sys module
2010+
- The PYTHONINTMAXSTRDIGITS environment variable
2011+
2012+
[Check the documentation](https://docs.python.org/3/library/stdtypes.html#int-max-str-digits) for more details on changing the default limit if you expect your code to exceed this value.
2013+
2014+
19792015
---
19802016
2017+
19812018
## Section: Slippery Slopes
19822019
19832020
### ▶ Modifying a dictionary while iterating over it

0 commit comments

Comments
 (0)