簡體   English   中英

python 中沒有 output 但 jupyter 筆記本中的 output

[英]no output in python but output in jupyter notebook

運行以下代碼

with open('abc', 'wt') as fh:
    fh.write('\n'.join(['a','b','c']))

ipython中,沒有預期的輸出; 但是,在jupyter notebook 中, output 像[Out 4]: 5

這可能與

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

在木星。 但是jupyter output有什么奇怪的,如何預防?

我不熟悉 jupyter,但如果它是一個 REPL,它在計算表達式的結果時打印它們,它會打印fh.write的返回值。 write返回寫入的字符數,以便打印出來。

_ = fh.write阻止 output 因為=是一個不計算值的語句。 您也可以通過執行fh.write(...); None fh.write(...); None 我不建議在實際代碼中這樣做,但為了限制 output,它可能是有益的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM