yield from () Was: Re: weirdness with list()

Larry Martell larry.martell at gmail.com
Tue Mar 2 18:06:53 EST 2021


On Tue, Mar 2, 2021 at 2:16 PM Chris Angelico <rosuav at gmail.com> wrote:
>
> On Tue, Mar 2, 2021 at 5:51 AM Alan Gauld via Python-list
> <python-list at python.org> wrote:
> >
> > On 28/02/2021 00:17, Cameron Simpson wrote:
> >
> > > BUT... It also has a __iter__ value, which like any Box iterates over
> > > the subboxes. For MDAT that is implemented like this:
> > >
> > >     def __iter__(self):
> > >         yield from ()
> >
> > Sorry, a bit OT but I'm curious. I haven't seen
> > this before:
> >
> > yield from ()
> >
> > What is it doing?
> > What do the () represent in this context?
> >
>
> It's yielding all the elements in an empty tuple. Which is none of
> them, meaning that - for this simple example - iterating over the
> object will produce zero results.

I discovered something new (to me) yesterday. Was writing a unit test
for generator function and I found that none of the function got
executed at all until I iterated on the return value. It was blowing
my mind as I was debugging the test and had a BP set in the first line
of the function but it was not hit when I called the function.


More information about the Python-list mailing list