Programmatically get current Ipython notebook cell output?

前端 未结 3 1716
梦毁少年i
梦毁少年i 2020-12-08 15:11

I have an imported function that runs in an IPython notebook (input cell X) which produces an output (in output cell X). After the function runs, I have some more code (also

3条回答
  •  庸人自扰
    2020-12-08 16:06

    IPython's output caching system defines several global variables:

    • [_] (a single underscore): stores previous output, like Python’s default interpreter.
    • [__] (two underscores): next previous.
    • [___] (three underscores): next-next previous.

    Additionally, after each output x is created, there is a variable _ created with the output as its value. For example:

    In [12]: lst = [i for i in range(11)]
    
    In [13]: lst
    Out[13]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    
    In [14]: _13
    Out[14]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    

    Also, if you're interested, _i contains the contents of the input cell x:

    In [15]: _i12
    Out[15]: 'lst = [i for i in range(11)]'
    

提交回复
热议问题