Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spyder does not show class instance variables in variable explorer during debugging python

Tags:

spyder

I am using Spyder v2.2.5 IDE for programming python. While debugging my python code using pdb in spyder, the IDE does not show the class instance variable in Variable Explorer. It's getting difficult to check the variable values using print statement every-time. Is there any way to check the class instance variable values during debugging?

like image 296
AlphaCoder Avatar asked Jul 12 '14 23:07

AlphaCoder


People also ask

How do you show variable in spyder Explorer?

Go to View/Panes and select Variable Explorer. Save this answer.

How do you show variables in spyder Python?

To see them all, double click the list to open a viewer that will display the index, type, size and value of each element of the list. Just like dictionaries, you can double-click values to edit them.

How do you refresh a variable Explorer in spyder?

To trigger a refresh, simply click the reload button on the Variable Explorer toolbar, or press the shortcut Ctrl+R ( Cmd-R ) when it has focus.


2 Answers

I know this is an old post, but I did find a temporary solution. Every class object has a dictionary associated with it that contains the assigned variables. It is a little annoying, but you can assign a global variable to be equal to that dictionary which can be viewed in Spyder's variable explorer.

import numpy as np 
class someClass:
    def __init__(self):
        self.var1=10     #integer type
        self.var2=np.ones((3,3,3))  #numpy array type
        self.var3=[np.ones((2,2,4))*i for i in range(5)]  #list type (of numpy arrays)

b=someClass()
tempdict=b.__dict__  #Then look at this variable under the Variable explorer

You will need to update tempdict every time you change any of the variables, but this will work.

like image 96
user3689374 Avatar answered Oct 20 '22 05:10

user3689374


As a temporary solution until they fix this, I am using local variables inside the method until the very end for debug. Use the same name, but without the "self." in front of it.

Right before the return statement, I assign the local variables to the "self." variable equivalent.

If I need the values of a prior, existing "self." variable during execution , then I assign it to a local variable in the beginning of the method.

After debug phase is complete, you can replace the locals with the proper class attribute.

like image 42
John Spongr Avatar answered Oct 20 '22 06:10

John Spongr