2016-08-21 71 views
7

我發現約在Python迭代器的行爲這個問題:QtConsole爲什麼沒有echo next()?

Python list iterator behavior and next(iterator)

當我鍵入的代碼:

a = iter(list(range(10))) 
for i in a: 
    print a 
    next(a) 

jupyter-qtconsole它返回:

0 
2 
4 
6 
8 

完全一樣Martijn Pieters表示,當口譯員不迴應的呼叫時應該這樣做。

然而,當我在我的Bash解釋和IDLE再次運行相同的代碼,該代碼印刷:

0 
1 
2 
3 
4 
5 
6 
7 
8 
9 

到控制檯。

我跑代碼:

import platform 
platform.python_implementation() 

在所有三個環境,他們都稱我跑'CPython'

那麼爲什麼QtConsole會在IDLE和Bash不行時抑制next(a)調用?

如果有幫助,我在Mac OSX上運行Python 2.7.9並使用Anaconda發行版。

回答

3

這只是IPython(其中QtConsole所基於的)的開發者所做出的選擇,該選項應該回饋給用戶。

具體而言,在使用的InteractiveShell類中,功能run_ast_nodes默認情況下使用interactivity='last_expr'定義。此屬性的文檔指出:

interactivity : str 
    'all', 'last', 'last_expr' or 'none', specifying which nodes should be 
    run interactively (displaying output from expressions). 'last_expr' 
    will run the last node interactively only if it is an expression (i.e. 
    expressions in loops or other blocks are not displayed. Other values 
    for this parameter will raise a ValueError. 

正如你可以看到:在循環或其他塊表達式不顯示

如果您真的需要,您可以在IPython的配置文件中更改此設置,並使其像repl一樣工作。要點是,這只是設計師的偏好。