[Python-ideas] Raise StopIteration with same value on subsequent `next`s

Yury Selivanov yselivanov.ml at gmail.com
Mon Dec 11 18:44:19 EST 2017


On Mon, Dec 11, 2017 at 6:21 PM, Franklin? Lee
<leewangzhong+python at gmail.com> wrote:
> What about hanging onto just the value, and creating new StopIteration
> instances instead of raising the same one again?

Doesn't really matter, as we're still prolonging the lifespan of the
returned object.

To me the current behaviour of generators seems fine.  For regular
generator users this is a non-problem.  For trampolines and async
frameworks--so many of them have been implemented and all of them
worked around this issue in one way or another.

> I would have attached the returned value to the generator object, but
> for some reason, generator and coroutine objects can't take
> attributes. Maybe I should ask for that feature instead.

You can use a WeakKeyDictionary to associate any state with a
generator; that should solve your problem.  We wouldn't want to add
__dict__ to generators to have another way of doing that.

Yury


More information about the Python-ideas mailing list