我在 Python 交互式控制台(不是空闲,不是 ipython)中定义了一个函数,我需要稍后再看很多屏。
例子:
>>> def myf():
... print 1
... print 2
... print 3
...
>>>
>>> # a lot of scrolling
>>>
Run Code Online (Sandbox Code Playgroud)
下面的假示例输出(而不是“名称'print_function'未定义”)来演示我想要的内容:
>>> print_function(myf)
myf()
print 1
print 2
print 3
Run Code Online (Sandbox Code Playgroud)
Python中有类似print_function的东西吗?如果没有,你将如何实施?
在交互式控制台中:
>>> import sys
>>> sys.stdout
<open file '<stdout>', mode 'w' at 0xb7810078>
>>> sys.stdout.close()
>>> sys.stdout # confirming that it's closed
(...) ValueError: I/O operation on closed file
Run Code Online (Sandbox Code Playgroud)
试图恢复:
>>> sys.stdout.open()
(...) AttributeError: 'file' object has no attribute 'open'
>>> sys.stdout.write('foo')
(...) ValueError: I/O operation on closed file
Run Code Online (Sandbox Code Playgroud)
我同意这是一个无聊的问题,但我很好奇如何在Python中恢复sys.stdout.close()(当然没有重新启动交互式控制台)以及为什么sys.stdout.open()没有意义.
python ×2