What is the difference between raise StopIteration and a return statement in generators?

前端 未结 3 1338
误落风尘
误落风尘 2020-12-08 13:08

I\'m curious about the difference between using raise StopIteration and a return statement in generators.

For example, is there any differe

3条回答
  •  Happy的楠姐
    2020-12-08 13:40

    That's true, they are equivalent except that one is readable whereas the other is obscure. This dates back to the very first version of generators (PEP 255, under "Specification: Return"), and the subsequent enhancements of (such as coroutines) do not change this. 3.3's yield from (PEP 380) extends that to return as syntactic sugar for raise StopIteration(), but that doesn't change the meaning of return;.

提交回复
热议问题