Mailing List Archive

"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
Re: "Invalid literal for int() with base 10": is it really a literal? [ In reply to ]
On Fri, 26 May 2023 at 17:56, Roel Schroeven <roel@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
--
https://mail.python.org/mailman/listinfo/python-list
Re: "Invalid literal for int() with base 10": is it really a literal? [ In reply to ]
Op 26/05/2023 om 10:29 schreef Chris Angelico:
> 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'
>
I was looking for other ValueError-generating functions to find
potentially better wording, and I somehow failed to think of float(), so
thank you for mentioning it :) The ones I could think of were math
functions like math.sqrt(); they give "ValueError: math domain error"
which is rather less user friendly.

--
"'How to Stop Worrying and Learn to Love the Internet':
1) everything that’s already in the world when you’re born is just
normal;
2) anything that gets invented between then and before you turn
thirty is incredibly exciting and creative and with any luck you can
make a career out of it;
3) anything that gets invented after you’re thirty is against the
natural order of things and the beginning of the end of civilisation
as we know it until it’s been around for about ten years when it
gradually turns out to be alright really.
Apply this list to movies, rock music, word processors and mobile
phones to work out how old you are."
-- Douglas Adams

--
https://mail.python.org/mailman/listinfo/python-list
Re: "Invalid literal for int() with base 10": is it really a literal? [ In reply to ]
On 2023-05-26 09:29, Chris Angelico wrote:
> On Fri, 26 May 2023 at 17:56, Roel Schroeven <roel@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'
>
You still need to mention the base because:

>>> int('y', 36)
34

--
https://mail.python.org/mailman/listinfo/python-list
RE: "Invalid literal for int() with base 10": is it really a literal? [ In reply to ]
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@python.org> On Behalf Of Roel Schroeven
Sent: Friday, May 26, 2023 3:55 AM
To: python-list@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

--
https://mail.python.org/mailman/listinfo/python-list
Re: "Invalid literal for int() with base 10": is it really a literal? [ In reply to ]
Chris Angelico wrote at 2023-5-26 18:29 +1000:
> ...
>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'

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