Issue 5018: Overly general claim about sequence unpacking in tutorial (original) (raw)

The datastructures section of the tutorial states with respect to "sequence unpacking" that "there is a small bit of asymmetry here: packing multiple values always creates a tuple, and unpacking works for any sequence". This is too general, as shown by the definition of assignment statement target in the simple_stmts section of the reference shows: tuples and lists are the only kinds of sequences that can appear on the left-hand side of an assignment statement.

I've read those paragraphs many times. Oddly enough when you asked me for a rewording and I went back and read them again I found a very different interpretation than all the other times. I've always thought of unpacking as being about the variables on the left rather than the values on the right.

Now that I am rethinking this I realize (a) that unpacking is always about both sides of the assignment and (b) the wording I criticized is actually precise and accurate, if a bit subtle. What I noticed this time is that "the sequence" in the sentence beginning "Sequence unpacking" refers to the sequence on the right, not the variables on the left, especially since "the variables on the left" are mentioned earlier in the sentence.

Part of how I backed into the wrong corner with this is that the left- hand side of an unpacking can be a list as well as a tuple. I was checking to see if any other kinds of sequences could go on the left, which of course they can't. That's why I thought "works for any sequence" was wrong. If only a tuple could be on the left I think I would have realized what this was saying sooner. It was the fact that more than one kind of sequence could go on the left that got be sidetracked.

Moreover, the example shows a target list and doesn't even show an explicit tuple or a list. So, it's being fairly informal, as is appropriate for a tutorial. However, with all that sorted out I now see a different problem: "multiple assignment is really just a combination of tuple packing and sequence unpacking". If there is an explicit tuple, or a list, on the left-hand side then there's no packing involved. In fact it's rather strange that one could put something like [x,y,z] on the left-hand side of the assignment. I suppose it must get converted during compilation to a tuple for multiple assignment -- it's not as if a list-valued variable could go there.

Sorry about the long-winded meditation. Hope the exploration proves interesting and or valuable to someone!