[Tutor] Regular User defined functions Vs Generator functions

Cameron Simpson cs at cskk.id.au
Sat Oct 31 21:00:08 EDT 2020


On 31Oct2020 23:41, Alan Gauld <alan.gauld at yahoo.co.uk> wrote:
>On 31/10/2020 17:54, Mats Wichmann wrote:
>> the caller is going to immediately consume the entire result of the
>> function, and there's potentially a large number of values, then there's
>> not really any point in having it be a generator,
>
>Yes but its hard to predict those ud cases.
>
>On the other side if the client may not consume all the
>values and break on finding the Nth item or one that matches
>some criteria then a generator makes perfect sense for
>large data sets.
>
>There is no single, simple answer. You need to understand
>the requirements and for 90% of the time it won't make much
>practical difference!

Yes, but there's a big caveat. Make sure the docstring says "returns an 
iterable".

Every so often I come down on the generator side of this choice, but 
call it expecting to get a sequence or other collection such as a set.  
But what I store is an unexecuted generator instance. Badness ensues 
later in the programme.

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


More information about the Tutor mailing list