views:

1683

answers:

7

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 would one go in python about that ?

+4  A: 

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.

Brian R. Bondy
+5  A: 

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.

Harold
Hi. This might work. Is there a way to get it to print it out in a relatively "vertical manner". Just flushing it out one after another isn't very helpful, expecially when having around 300 variables.
ldigas
Sure, try this:import sys, pprintsys.displayhook = pprint.pprintlocals()
Harold
oops, that didn't format too well. I'll add it to my answer.
Harold
or form pprint import pprint, then use pprint instead of print
hasen j
+1 for sys.displayhook. Many thanks!
EOL
+14  A: 

Couple of 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
Cannonade
+2  A: 

globals(), locals(), vars(), and dir() may all help you in what you want.

Devin Jeanpierre
+4  A: 

keep in mind dir() will return all current imports, AND variables.

if you just want your variables, I would suggest a naming scheme that is easy to extract from dir, such as varScore, varNames, etc.

that way, you can simply do this:

for vars in dir():
 if vars.startswith("var"):
   print vars

Edit

if you want to list all variables, but exclude imported modules and variables such as:

__builtins__

you can use something like so:

import os
import re

x = 11
imports = "os","re"

for vars in dir():
    if vars.startswith("__") == 0 and vars not in imports:
     print vars

as you can see, it will show the variable "imports" though, because it is a variable (well, a tuple). A quick workaround is to add the word "imports" into the imports tuple itself!

John T
Uhmm, no, that won't be possible I'm afraid. Most of them are just t, a, c, d, L, p ... and such.
ldigas
Ah, a small project im guessing? I'd go insane making large projects using single character variables, once you get to 1k+ LOC and you have to remember what each variable holds... it gets frustrating.
John T
My guess it will be about 300 pg report, so that makes it about 2000 variables (thickness of this, thickness of that, thickness of something I don't even know what it is .... :) ... yes it does.
ldigas
+3  A: 

If this is an option for you, you might want to look at IPython:

To get a list of all currently defined variables, type who:

In [1]: foo = 'bar'

In [2]: who
foo

You can type whos for more detail:

In [3]: whos
Variable   Type    Data/Info
----------------------------
foo        str     bar

There are a wealth of other functions available - basically it is the Python interpreter on steroids. One convenient one is store command, which lets you save variables between sessions (kind of like a super easy pickle)

If you have a few minutes, check out Jeff Rush's excellent IPython demonstration screencasts:

I am in no way associated with the team behind IPython, just a satisfied user.

Chris Lawlor
+1, but wishing I could vote +10: IPython's %who removes non-user variables from locals()!
EOL
+1  A: 

Already been asked, I posted this a while back, it's at:

Enumerate or list all variables in a program of [your favorite language here]

Kurt
Exactly what I was looking for (although John made a nice point of how to avoid imported objects).
ldigas