Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View Variables in Python

Is there a way to view a list of all of my variables in python while the program is running without setting breakpoints? Printing is too messy because I have a lot of variables that are constantly changing.

Thanks

like image 622
Digital Hazard Avatar asked Nov 24 '11 07:11

Digital Hazard


2 Answers

Maybe inspect helps you, but you have to filter the information then.

Usage like:

> import inspect
> a = 5
> f = inspect.currentframe()
> print f.f_locals
...
...
'a': 5
...

Maybe it's worth to mention that you cannot iterate over the resulting dictionary in a for loop because asignment to a variable would change that dictionary. You have to iterate only over the keys (at least that's what I just found out).

Example:

for v in f.f_locals.keys():
    if not v.startswith("_"):
        print v

Look at the first line: simply writing for v in f.f_locals would not succeed.

like image 64
wal-o-mat Avatar answered Nov 05 '22 08:11

wal-o-mat


If you are running on Pydev (python extension for eclipse), you can easily watch your variables, however you'll need to set a breakpoint initially, then only step into/over your code.

like image 32
Sumit Bisht Avatar answered Nov 05 '22 08:11

Sumit Bisht