Unpacking nothing in Python

June 10, 2015, 6:56 p.m.

In his presentation on concurrency during PyCon, David Beazley did this:

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

He meant to put _ where the first [] is. Essentially, he invoked this:

[] = []

This works! Thinking about this in terms of unpacking, it sort of makes sense. Since the left-hand side has no identifiers to unpack to, the right-hand side should have no values to unpack.

[] = [1] # ValueError: too many values to unpack (expected 0)
[x] = [] # ValueError: need more than 0 values to unpack

Since unpacking works with iterables, naturally these are legal as well:

[] = ()
[] = {}
[] = range(0)
...

But not this:

() = () # SyntaxError

In tuple unpacking, the outer parentheses in the left-hand side are redundant. Maybe this is treated as if there is nothing on the left-hand side?