ValueError vs IndexError, unpacking arguments with string.split
Morten W. Petersen
morphex at gmail.com
Fri Nov 30 10:17:43 EST 2018
On Fri, Nov 30, 2018 at 4:02 PM Peter Otten <__peter__ at web.de> wrote:
> Morten W. Petersen wrote:
>
> > I've been reading up on a bit of C++, Assembler etc. lately, so maybe my
> > mind expected an IndexError because of that, but isn't it logical that
> the
> > string is parsed and split, and then later the unpacking operation fails
> > with an IndexError?
>
> You might think that
>
> a, b = c
>
> is equivalent to
>
> a = c[0]
> b = c[1]
>
> but the c above can be an arbitrary iterable:
>
> >>> a, b = iter("ab")
> >>> a, b
> ('a', 'b')
> >>> a, b = iter("abc")
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> ValueError: too many values to unpack (expected 2)
>
> So there is not necessarily a lookup by index.
>
Ah, of course. OK, right you are. :)
-Morten
--
Videos at https://www.youtube.com/user/TheBlogologue
Twittering at http://twitter.com/blogologue
Blogging at http://blogologue.com
Playing music at https://soundcloud.com/morten-w-petersen
Also playing music and podcasting here:
http://www.mixcloud.com/morten-w-petersen/
On Google+ here https://plus.google.com/107781930037068750156
On Instagram at https://instagram.com/morphexx/
More information about the Python-list
mailing list