开发者

Python 3.2 Idle vs terminal

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 co开发者_运维百科mmand 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?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜