如何抑制IPython Notebook中的输出? [英] How do you suppress output in IPython Notebook?

查看:58
本文介绍了如何抑制IPython Notebook中的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何抑制输出到stdout?

例如,分号可用于禁止显示返回的对象

A semi-colon can be used to supress display of returned objects, for example

>>> 1+1
2

>>> 1+1;   # No output!

但是,打印到stdout的函数不受分号的影响.

However, a function that prints to stdout is not affected by the semi-colon.

>>> print('Hello!')
Hello!

>>> MyFunction()
Calculating values...

如何抑制print/MyFunction的输出?

推荐答案

添加%%capture作为单元格的第一行.例如

Add %%capture as the first line of the cell. eg

%%capture
print('Hello')
MyFunction()

这只是丢弃输出,但是%%capture魔术可以用于将输出保存到变量-

This simply discards the output, but the %%capture magic can be used to save the output to a variable - consult the docs

这篇关于如何抑制IPython Notebook中的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆