sys.stdout 與 print
當我們在 Python 中打印對象調用 print obj 時候,事實上是調用了 sys.stdout.write(obj+'\n')
print 將你需要的內容打印到了控制台,然后追加了一個換行符
print 會調用 sys.stdout 的 write 方法
以下兩行在事實上等價:
sys.stdout.write('hello'+'\n') print 'hello'
sys.stdin 與 raw_input
當我們用 raw_input('Input promption: ') 時,事實上是先把提示信息輸出,然后捕獲輸入
以下兩組在事實上等價:
hi=raw_input('hello? ') print 'hello? ', #comma to stay in the same line hi=sys.stdin.readline()[:-1] # -1 to discard the '\n' in input stream