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

Chris Angelico rosuav at gmail.com
Fri May 26 04:29:58 EDT 2023


On Fri, 26 May 2023 at 17:56, Roel Schroeven <roel at roelschroeven.net> wrote:
>
> 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?
>

It's a ValueError, so the problem is with the value. I suppose
"invalid notation" might work, but since the definition of what's
acceptable to the int() constructor is the same as for a Python
literal, it's not wrong to use that word.

However, if you want to change the wording, I'd be more inclined to
synchronize it with float():

>>> float("a")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: 'a'

ChrisA


More information about the Python-list mailing list