Discuss / Python / 整理一下思路

整理一下思路

Topic source

反草

#1 Created at ... [Delete] [Delete and Lock User]

其实把

        if not n:
            return

代码删掉,结果是一样的,通过测试和查看api总结出:

The execution starts when one of the generator’s methods is called. At that time, the execution proceeds to the first yield expression, where it is suspended again, returning the value of expression_list to the generator’s caller.

c.send(None)其实等价于next(c),第一次执行时其实只执行到n = yield r就停止了,然后把r的值返回给调用者;然后c.send(1)把1传给yield r,所以这时n的值为1;然后接着上次停止的地方继续,到n = yield r时又停止并把r的值返回给调用者,如此循环。

可否解释下这个if not n: return的作用?这句话没太看懂,搜了一下也没有具体的解释。

说得对,在这个例子不可能运行到这里,写了之后好像是防止n == None的,有误导之嫌

楼上的两位可以看下 灵剑 的这个回答: http://zhihu.com/question/48707732/answer/112233903


  • 1

Reply