Python 3.2 Idle vs terminal

◇◆丶佛笑我妖孽 提交于 2019-12-10 17:02:46

问题


In python 3.2 under OSX, if I'll run "type(sys.stdin)" under Idle I get a strange answer as shown below

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

But if I'll reun the same command under terminal, I get:

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

I understand this is because I'm running it under IDLE. but is this not misleading?

I was trying to run the following commands in IDLE and spent hours trying to understand as to why this is not working. (I'm still a python noob)

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

But just discovered that I works fine under terminal.

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

Is this a bug?


回答1:


This is a filed 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.


Note that this is probably not fixed because readlines is not normally useful. Instead, you can just iterate over the file objects itself:

for line in sys.stdin:
    ...


来源:https://stackoverflow.com/questions/6244956/python-3-2-idle-vs-terminal

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