Quantcast
Channel: Print all variables defined in one Jupyter cell - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Answer by umutto for Print all variables defined in one Jupyter cell

$
0
0

You can use whos command to see all variables stored in the current kernel.

k, g, lx = .4, .8, 6.6m = k*g*lx**2whos

outputs:

Variable   Type     Data/Info-----------------------------g          float    0.8k          float    0.4lx         float    6.6m          float    13.939200000000001

But as said, it displays all variables, so it will display other variables from earlier cells you've run.

A similar result can be achieved using locals() or globals() command from python built-in functions, which return a dictionary of variables. But the way jupyter represents is prettier.


Alternatively you can use InteractiveShell. This will change the behavior of cells and act like a python shell would, so it will output every called value (to output cell) once run.

from IPython.core.interactiveshell import InteractiveShellInteractiveShell.ast_node_interactivity = "all"kg... do stuff ...lxm... do more stuff ...

outputs:

Out[2]: 0.4Out[2]: 0.8Out[2]: 6.6Out[2]: 13.939200000000001

And finally you can return the interactivity to default by setting it to last_expr.

InteractiveShell.ast_node_interactivity = "last_expr"

But the way you do it is probably the easiest and prettiest way, you can just remove the assignment on dataframe to make it a one liner or you can make it more compact to call by:

k, g, lx, mOut[3]: (0.4, 0.8, 6.6, 13.939200000000001)

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>