View variables in Python

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

thanks

+4
source share
4 answers

Maybe inspect will help you, but then you need to filter out the information.

Using:

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

It might be worth mentioning that you cannot iterate over the resulting dictionary in a for loop, because binding to a variable will change that dictionary. You should only iterate over the keys (at least what I just found out).

Example:

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

Look at the first line: just writing for v in f.f_locals will fail.

+4
source

If you are working with Pydev (the python extension for eclipse), you can easily view your variables, however, you first need to set a breakpoint, and then just paste in / on your code.

+2
source

Take the debugger: http://winpdb.org/ , and then you can look at them and see them there; however, if you are just looking for a function to print all the variables defined in the local scope, it simply calls locals() .

+1
source

If your question refers to python as a language (as opposed to "python as an ecosystem of applications and utilities"), AFAIK, the answer is no (in any case, not out of the box). Of course, there are a number of IDEs that allow you to use debug to view variable values ​​[see Sumit , for example].

However, if during development you need a "live monitor" of several variables, you can use the logging module and define your own Handler class to redirect the information live somewhere. Setting the message to the log.debug level will result in the trivial activation / deactivation of this function, simply by changing the minimum log level of your application.

NTN!

+1
source

Source: https://habr.com/ru/post/1382836/


All Articles