[Python-ideas] x=(yield from) confusion [was:Yet another alternative name for yield-from]

Jim Jewett jimjjewett at gmail.com
Fri Apr 3 18:48:38 CEST 2009


On 4/3/09, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Greg tried to clarify this a bit already, but I think Jacob's averager
> example is an interesting case where it makes sense to both yield
> multiple times and also "return a value".

>   def averager(start=0):
>     # averager that maintains a running average
>     # and returns the final average when done
>     count = 0
>     exc = None
>     sum = start
>     while 1:
>       avg = sum / count
>       try:
>         val = yield avg
>       except CalcAverage:
>         return finally avg
>       sum += val
>       count += 1

It looks to me like it returns (or yields) the running average either way.

I see a reason to send in a sentinel value, saying "Don't update the
average, just tell me the current value."

I don't see why that sentinel has to terminate the generator, nor do I
see why that final average has to be returned rather than yielded.

-jJ



More information about the Python-ideas mailing list