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

Cameron Simpson cs at cskk.id.au
Mon Mar 1 16:59:16 EST 2021


On 28Feb2021 23:47, Alan Gauld <alan.gauld at yahoo.co.uk> 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 an empty tuple. The yield from iterates over the tuple, yielding 
zero times. There are shorter ways to write that (eg outright omitting 
the yield), except when you're writing a generator function with only a 
single yield statement - then you need something like that to make it a 
generator.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list