Вопрос

Is it possible to view namespaces other than Global in the Spyder variable explorer? I'd like to clean up my code to get things into main(), but I still need to take a look at variable contents at this stage in development.

EDIT: Assume my script currently only contains:

a = 2
b = 4
c = a + b

I run this in Spyder, I can use the variable explorer post-run to view a, b, and c. Now if I wrap this in main(), per less-bad coding practice:

def main():
   a = 2
   b = 4
   c = a + b

Now I can no longer view them post-run in the variable explorer. (The comment suggests a breakpoint, but this is being used to interface to an instrument in real-time, so I can't pause execution). /end EDIT.

Are my only choices to either (1) leave the code with variables I need to look at outside of a function or (2) export the variables I'll want to examine into the global namespace from main()? Either solution leaves me with something to do/undo later.

Это было полезно?

Решение

Sorry, this is not possible at the moment with Spyder. This is the closest thing you can do to get the result you want:

import inspect

local_vars = {}

def main():
    global local_vars
    a = 2
    b = 4
    c = a+b
    local_vars = inspect.currentframe().f_locals
    return c

main()

After running it you should see that the local_vars dictionary contains the local variables defined inside the main function.

Note: After giving double-click to local_vars contents, Spyder will open a new window which is basically equivalent to the view shown in our variable explorer but only with your local variables.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top