Attempting to understand yield as an expression

后端 未结 5 1761
猫巷女王i
猫巷女王i 2021-01-18 14:59

I\'m playing around with generators and generator expressions and I\'m not completely sure that I understand how they work (some reference material):

>>         


        
5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-18 15:24

    The generator you wrote is equivalent to the more verbose:

    def testing():
        for x in range(10):
                x = (yield x)
                yield x
    

    As you can see here, the second yield, which is implicit in the generator expression, does not save the value you pass it, therefore depending on where the generator execution is blocked the send may or may not work.

提交回复
热议问题