2

Partway through this PyCon talk, the speaker types out a very unusual line of code:

can_recv, can_send, [] = select(recv_wait, send_wait, [])

Everything proceeds smoothly, but then at the end, when an audience member asks him why he put an empty list literal on the left-hand side of the unpack assignment, he gets all surprised. "Did I do that?" When he finds it, he states that he meant to use a _, denoting a throwaway variable, and it seems that he has no idea why that even worked at all.

Can anyone explain what's going on here, how that ran smoothly rather than crashing with a runtime error?

Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477

1 Answers1

1

You can unpack into a list, so long as the list lengths match. For instance:

can_recv, can_send, [a, b] = 1, 2, [3, 4]

will assign a=3 and b=4.

The case in that lecture is the degenerate case of null lists. BTW, you can do this with an empty tuple or an empty dictionary on the right side just as well.

Prune
  • 76,765
  • 14
  • 60
  • 81