Generator and return value
Cameron Simpson
cs at zip.com.au
Wed Jun 7 06:32:18 EDT 2017
On 07Jun2017 19:19, Steve D'Aprano <steve+python at pearwood.info> wrote:
>> Frank Millman writes:
>>> It would be nice to write a generator in such a way that, in addition
>>> to 'yielding' each value, it performs some additional work and then
>>> 'returns' a final result at the end.
>>>
>>>> From Python 3.3, anything 'returned' becomes the value of the
>>>> StopIteration
>>> exception, so it is possible, but not pretty.
>>>
>>> Instead of -
>>> my_gen = generator()
>>> for item in my_gen():
>>> do_something(item)
>>> [how to get the final result?]
>
>Currently, I don't believe there is a way.
I sometimes yield what would be a return value as the final item. Not very
happy with it though.
Cheers,
Cameron Simpson <cs at zip.com.au>
More information about the Python-list
mailing list