用于抑制输出的分号在 IPython 中不起作用

发布于 2024-12-06 01:28:19 字数 785 浏览 1 评论 0原文

IPython Tips & 的文档中Tricks,它说在命令末尾放置一个分号(;)以抑制其输出。这在我的情况下似乎不起作用,即使是

print('Hello');

输出

Hello

我对输出抑制有错误的想法还是这是一个错误?在 PuDB 中工作时,这尤其烦人,因为当我按“下一步”时,它会在我的情况下可怕地闪烁”或“步入”。

PS:输出既不在我的 Ubuntu IPython 0.10 上,也不在 OS X v10.7 (狮子)IPython 0.11 被压制。尽管闪烁问题在 OS X 中更严重,可能是因为 item2。

In the documentation at IPython Tips & Tricks, it says to put a semicolon (;) at the end of a command to suppress its output. This does not seem to work in my case as even a

print('Hello');

outputs

Hello

Do I have the wrong idea of output suppression or is this a bug? This is especially annoying when working in PuDB, as it flashes horribly in my case as I press 'next' or 'step into'.

P.S.: The output is neither on my Ubuntu IPython 0.10 nor OS X v10.7 (Lion) IPython 0.11 suppressed. Although the flashing issue is worse in OS X, probably because of item2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

咋地 2024-12-13 01:28:19

尝试类似1 + 1;。如果没有分号,它应该通过打印结果来为您提供有关结果的反馈(由 repr 格式化,尽管在整数的情况下并不重要) - 我认为这个输出应该是压制。

shell 不会(也不应该)抑制对 sys.stdout 引用的文件的写入(这本质上是 print 所做的)。这是完全不同的事情,不是 shell 的工作。

Try something like 1 + 1;. Without the semicolon, it should give you feedback about the result by printing it (formatted by repr, though it doesn't matter in the case of integers) - I assume that it's this output that's supposed to be suppressed.

The shell doesn't (and shouldn't) suppress writing to the file that happens to be referenced by sys.stdout (which is essentially what print does). This is an entirely different matter, and not the job of the shell.

你的呼吸 2024-12-13 01:28:19

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

%%capture
print('Hello')

这只是丢弃输出,但可以使用 %%capture 魔法将输出保存到变量中 - 查阅文档

Add %%capture as the first line of the cell. For example,

%%capture
print('Hello')

This simply discards the output, but the %%capture magic can be used to save the output to a variable—consult the documentation.

滿滿的愛 2024-12-13 01:28:19

这是 Dataquest 中的另一个示例 - 28 个 Jupyter Notebook 提示、技巧和快捷方式帖子:

  # Use a semicolon to suppress the output of a final function.
  %matplotlib inline
  from matplotlib import pyplot as plt
  import numpy
  x = numpy.linspace(0, 1, 1000)**1.5
  plt.hist(x); # Output not suppressed w/ semicolon?

以及“有效”分号抑制的示例:

x = 1 + 1
x; # Output suppressed with semicolon!

因此它似乎抑制了通常显示在终端中的for语句,但不是“内联”类型,如情节。

Here's another example from the Dataquest — 28 Jupyter Notebook tips, tricks, and shortcuts post:

  # Use a semicolon to suppress the output of a final function.
  %matplotlib inline
  from matplotlib import pyplot as plt
  import numpy
  x = numpy.linspace(0, 1, 1000)**1.5
  plt.hist(x); # Output not suppressed w/ semicolon?

And an example of a "working" semicolon suppression:

x = 1 + 1
x; # Output suppressed with semicolon!

So it appears to suppress for statements that would normally show up in the terminal, but not "inline" types, like plots.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文