IPython and REPL behave differently when displaying data without the print function

前端 未结 1 1682
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-14 01:49

Note that all experiments have been performed on Python3.4.3 and IPython 5.1.0 (for python3).


Consider a function that returns the identity:

de         


        
1条回答
  •  旧巷少年郎
    2021-01-14 02:32

    For how it works, IPython compiles loops in 'exec' mode instead of 'single', so sys.displayhook is not triggered for expression statements inside a loop. The regular interactive interpreter executes anything you enter in 'single' mode. 'single' mode is the mode where expression statements trigger sys.displayhook.

    For why IPython does this, the regular Python behavior is more annoying than useful. You rarely want to auto-print the values of expression statements in a loop; more frequently, it'll happen by accident and scroll things you're interested in off the screen.

    IPython tries to provide more useful behavior. It's much more intuitive to explicitly print the things you want printed than to explicitly suppress the things you don't want printed.

    0 讨论(0)
提交回复
热议问题