gpt4 book ai didi

Python 3.2 空闲与终端

转载 作者:太空狗 更新时间:2023-10-30 02:23:08 25 4
gpt4 key购买 nike

在 OSX 下的 python 3.2 中,如果我在 Idle 下运行“type(sys.stdin)”,我会得到一个奇怪的答案,如下所示

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

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

>>> import sys
>>> type(sys.stdin)
<class '_io.TextIOWrapper'>
>>>

我明白这是因为我在 IDLE 下运行它。但这不是误导吗?

我试图在 IDLE 中运行以下命令,并花了数小时试图理解为什么这不起作用。 (我还是个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']
>>>

这是一个错误吗?

最佳答案

这是一个已归档的 Python 错误:

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:
...

关于Python 3.2 空闲与终端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6244956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com