Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #94543

Re: scalar vs array and program control

References <spluque@gmail.com> <87vbd850qa.fsf@gmail.com> <201507251101.t6PB1LqE021165@fido.openend.se>
Date 2015-07-25 21:34 +1000
Subject Re: scalar vs array and program control
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.975.1437824061.3674.python-list@python.org> (permalink)

Show all headers | View raw


On Sat, Jul 25, 2015 at 9:01 PM, Laura Creighton <lac@openend.se> wrote:
> How did I know to look for ValueErrors?
>
>>>> int("1.2")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   ValueError: invalid literal for int() with base 10: '1.2'
>
> Cause that is what Python gives you.  If it had given you a TypeError
> instead -- which is what I expected it to do, but so it goes -- I
> would have said except TypeError.

The difference between "12", which is valid input for int(), and
"1.2", which isn't, is not the type of the object (both are str), but
their values. That's why the exception is a ValueError. If you pass it
a completely invalid *type*, then you get this:

>>> int([])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a
number, not 'list'

ChrisA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: scalar vs array and program control Chris Angelico <rosuav@gmail.com> - 2015-07-25 21:34 +1000

csiph-web