I'm currently working on a computation in python shell. What I want to have is Matlab style listout where you can see all the variables that have been defined up to a point (so I know which names I've used, their values and such).
Is there a way, and how can I do that?
In Python, you can return multiple values by simply return them separated by commas.
Use %reset to Clear All Variables in IPython - Requires User Confirmation. Typing in y deletes all the variables.
A few things you could use:
dir()
will give you the list of in scope variables:globals()
will give you a dictionary of global variableslocals()
will give you a dictionary of local variablesIf possible, you may want to use IPython.
To get a list of all current user-defined variables, IPython provides a magic command named who
(magics must be prefixed with the %
character unless the automagic
feature is enabled):
In [1]: foo = 'bar' In [2]: %who foo
You can use the whos
magic to get more detail:
In [3]: %whos Variable Type Data/Info ---------------------------- foo str bar
There are a wealth of other magics available. IPython is basically the Python interpreter on steroids. One convenient magic is store
, which lets you save variables between sessions (using pickle
).
Note: I am not associated with IPython Dev - just a satisfied user.
Edit:
You can find all the magic commands in the IPython Documentation.
This article also has a helpful section on the use of magic commands in Jupyter Notebook
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With