"Invalid literal for int() with base 10": is it really a literal?

avi.e.gross at gmail.com avi.e.gross at gmail.com
Fri May 26 12:22:57 EDT 2023


Roel,

In order for the code to provide different error messages, it needs a way to differentiate between circumstances. 

As far as the int() function is concerned, it sees a string of characters and has no clue where they came from. In Python, int(input()) just runs input() first and creates a string and then passes it along to int().

You can of course argue there are ways to phrase an error message that may be less technicalese.

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com at python.org> On Behalf Of Roel Schroeven
Sent: Friday, May 26, 2023 3:55 AM
To: python-list at python.org
Subject: "Invalid literal for int() with base 10": is it really a literal?

Kevin M. Wilson's post "Invalid literal for int() with base 10?" got me 
thinking about the use of the word "literal" in that message. Is it 
correct to use "literal" in that context? It's correct in something like 
this:

 >>> int('invalid')
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'invalid'

But something like this generates the same message:

 >>> int(input())
hello
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'hello'

In cases like this there is no literal in sight.

I'm thinking it would be more correct to use the term 'value' here: 
ValueError: invalid value for int() with base 10: 'hello'
Does my reasoning make sense?

-- 
"I love science, and it pains me to think that to so many are terrified
of the subject or feel that choosing science means you cannot also
choose compassion, or the arts, or be awed by nature. Science is not
meant to cure us of mystery, but to reinvent and reinvigorate it."
         -- Robert Sapolsky

-- 
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list