ValueError vs IndexError, unpacking arguments with string.split

Dan Sommers 2QdxY4RzWzUUiLuE at potatochowder.com
Fri Nov 30 12:21:05 EST 2018


On 11/30/18 10:57 AM, Morten W. Petersen wrote:
 > On Fri, Nov 30, 2018 at 4:25 PM Dan Sommers 
<2QdxY4RzWzUUiLuE at potatochowder.com> wrote:

 >> Python validates that the right hand side contains exactly the right
 >> number of elements before beginning to unpack, presumably to ensure a
 >> successful operation before failing part way through.

 > Hm.  Well I like the simplicity and abstraction of Python, it makes it
 > a very productive language.

Agreed, and hold that thought.

 > But since you mention it, why is it necessary to ensure a successful
 > operation?
 >
 > Is it so that when
 >
 > a,b,c = [1,2]
 >
 > fails, none of the variables a,b,c have been assigned to, and because
 > of that, one avoids "rolling back" any assignment that would have been
 > done without checking the right-hand argument first ?

That's what I was getting at, but apparently failed to express clearly.

Yes, there are use cases for a short iterator just not assigning the
rest of the variables, and for a long iterator using only what it needs,
but also explicit is better than implicit, and errors should never pass
silently, and other bits of wisdom learned from horrible debugging
experiences.



More information about the Python-list mailing list