Viewing all defined variables [duplicate]
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?
Solution 1:
A few things you could use:
-
dir()
will give you the list of in scope variables: -
globals()
will give you a dictionary of global variables -
locals()
will give you a dictionary of local variables
Solution 2:
If 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
Solution 3:
To get the names:
for name in vars().keys():
print(name)
To get the values:
for value in vars().values():
print(value)
vars() also takes an optional argument to find out which vars are defined within an object itself.
Solution 4:
As RedBlueThing and analog said:
-
dir()
gives a list of in scope variables -
globals()
gives a dictionary of global variables -
locals()
gives a dictionary of local variables
Using the interactive shell (version 2.6.9), after creating variables a = 1
and b = 2
, running dir()
gives
['__builtins__', '__doc__', '__name__', '__package__', 'a', 'b']
running locals()
gives
{'a': 1, 'b': 2, '__builtins__': <module '__builtin__' (built-in)>, '__package__': None, '__name__': '__main__', '__doc__': None}
Running globals()
gives exactly the same answer as locals()
in this case.
I haven't gotten into any modules, so all the variables are available as both local and global variables. locals()
and globals()
list the values of the variables as well as the names; dir()
only lists the names.
If I import a module and run locals()
or globals()
inside the module, dir()
still gives only a small number of variables; it adds __file__
to the variables listed above. locals()
and globals()
also list the same variables, but in the process of printing out the dictionary value for __builtin__
, it lists a far larger number of variables: built-in functions, exceptions, and types such as "'type': <type 'type'>
", rather than just the brief <module '__builtin__' (built-in)>
as shown above.
For more about dir()
see Python 2.7 quick reference at New Mexico Tech or the dir() function at ibiblio.org.
For more about locals()
and globals()
see locals and globals at Dive Into Python and a page about globals at New Mexico Tech.
[Comment: @Kurt: You gave a link to enumerate-or-list-all-variables-in-a-program-of-your-favorite-language-here but that answer has a mistake in it. The problem there is: type(name)
in that example will always return <type 'str'>
. You do get a list of the variables, which answers the question, but with incorrect types listed beside them. This was not obvious in your example because all the variables happened to be strings anyway; however, what it's returning is the type of the name of the variable instead of the type of the variable. To fix this: instead of print type(name)
use print eval('type(' + name + ')')
. I apologize for posting a comment in the answer section but I don't have comment posting privileges, and the other question is closed.]
Solution 5:
print locals()
edit continued from comment.
To make it look a little prettier when printing:
import sys, pprint
sys.displayhook = pprint.pprint
locals()
That should give you a more vertical printout.