IPython, semicolon to suppress output does not work

旧时模样 提交于 2019-12-23 12:33:02

问题


In the documentation at http://ipython.org/ipython-doc/dev/interactive/tips.html 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');  
--> '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 osx lion ipython 0.11 supressed. Although the flashing issue is worse in osx, probably because of item2.


回答1:


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.




回答2:


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

%%capture
print('Hello')

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




回答3:


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 "working" semicolon suppression:

x = 1 + 1
x; # Output suppressed w/ semicolon!

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



来源:https://stackoverflow.com/questions/7500754/ipython-semicolon-to-suppress-output-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!