简体   繁体   中英

How can I step through python code to find when a variable is modified?

I am writing a python program which defines a class, SpatialPooler. Objects of this class have an internal Boolean variable, self._learn and another internal variable self._data. When self._learn==False, self._data should not be altered by the SpatialPooler's main method.

I have another python file which creates an object instantiating this class and then runs through a test scenario. With learning turned on, the output is as expected. However, when learning is turned off, something goes wrong and self._data is modified, but I cannot find where in the code this is happening.

Is there a way to step through the code to find where this variable is being modified? pdb seems like it may be the kind of tool that would allow me to do this, but my code is not actually throwing any warnings or errors, and I can't find any instructions on how I could accomplish finding the function that is inappropriately modifying the self._data variable.

Any suggestions or tips would be much appreciated.

It's a little difficult to tell you where exactly to put this, but you can add a pdb statement somewhere in the code.

Specifically, add import pdb; pdb.set_trace() import pdb; pdb.set_trace() somewhere in the code (maybe the main function you mentioned), and when the program gets to that line, it will go into the debugger. You can then step through the code as it executes (by entering n ). You can see what the variable values are by just typing the variable name ( self._data , if you're in the class's method).

Reference to how pdb is used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM