Python3.2空闲与终端

2024-10-01 19:20:03 发布

您现在位置:Python中文网/ 问答频道 /正文

在OSX下的Python3.2中,如果我运行“type(标准输入)“在空闲状态下,我得到一个奇怪的答案,如下所示

>>> type(sys.stdin)
<class 'idlelib.rpc.RPCProxy'>
>>> 

但如果我在终端下重新执行同样的命令,我会得到:

^{pr2}$

我明白这是因为我在空闲状态下运行。但这不是误导吗?在

我试图在空闲状态下运行以下命令,并花了数小时试图理解为什么这不起作用。(我还是个Python呆子)

>>> w = sys.stdin.readlines()
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    w = sys.stdin.readlines()
AttributeError: readlines

但刚发现我在终端下工作得很好。在

>>> w = sys.stdin.readlines()
wow
ww
wewew
>>> 
>>> w
['wow\n', 'ww\n', 'wewew\n']
>>> 

这是虫子吗?在


Tags: 答案命令终端标准状态typestdinsys
1条回答
网友
1楼 · 发布于 2024-10-01 19:20:03

这是一个归档的Python bug:

http://bugs.python.org/issue9290

The fact that in IDLE sys.stdin is a idlelib.rpc.RPCProxy results in programs having different behavior in IDLE and in Command Line mode.

I noticed that when grading many students exercises in IDLE. Things like:

sys.stdin.readlines()

just don´t exists in IDLE, but are fully operational in Command Line mode.

In Command Line mode, sys.stdin is a file.

This is expected, as the manual (27.1) says that sys.stdin (and stdout and stderrr) are "File objects corresponding to the interpreter’s standard input"

There are also other "quirks".

I fell that is really strange that stdin has different behavior for the same program.


请注意,这可能不是固定的,因为readlines通常不起作用。相反,您可以只迭代文件对象本身:

for line in sys.stdin:
    ...

相关问题 更多 >

    热门问题