为什么“val=yield x”在一个单独的输出单元中单独运行,val为x?

问题描述 投票:0回答:1

这是我写的代码:

def genFunc():
    while True:
        val = yield True
        print(val)

myGen = genFunc()
next(myGen)
next(myGen)
myGen.send("Sending a message!")
next(myGen)
next(myGen)

在 .py 文件中运行它会返回以下内容:

None
Sending a message!
None
None

在 jupyter 单元(或 vscode python 交互式环境)中运行它会将其返回为两个单独的输出块:

None
Sending a message!
None
None
True

我没有任何合理的解释为什么会发生这种情况

python python-3.x jupyter-notebook jupyter
1个回答
0
投票

发生这种情况是因为

sys.displayhook

© www.soinside.com 2019 - 2024. All rights reserved.