[]=[]

Chris Angelico rosuav at gmail.com
Mon Sep 25 12:56:39 EDT 2023


On Tue, 26 Sept 2023 at 02:52, Piergiorgio Sartor via Python-list
<python-list at python.org> wrote:
>
> On 23/09/2023 09.41, Stefan Ram wrote:
> > ram at zedat.fu-berlin.de (Stefan Ram) writes:
> >> []=[]
> >
> >    I was watching a video of a David Beazley talk "Python
> >    Concurrency From the Ground Up" , where he wrote
> >
> > can_recv, can_send, [] = select(recv_wait, send_wait, [])
> >
> >    . Later, he clarified that he actually wanted to write
> >
> > can_recv, can_send, _ = select(recv_wait, send_wait, [])
> >
> >    and that he was surprised how the "[]" gave no error.
> >    ("I wonder why that works.")
>
> If you try:
>
> [] = [1]
>
> and check the error, it will be clear how it works.
> Maybe not why... :-)
>

Note that the reason it gives no error is that select() returned an
empty iterable as the third value. And you can be sure that it will
ALWAYS return an empty iterable, because select() returns three values
that correspond to the three parameters, and are subsets of them -
that is to say, everything in can_recv must have previously been in
recv_wait, and everything in can_send must have been in send_wait.
Since the third (waiting for exceptional conditions) was empty, there
can't ever be anything to return, and so [] will work, and unpack zero
elements.

ChrisA


More information about the Python-list mailing list