I know there are a thousand posts on debugging in Python but I can't find what I am looking for....a visual debugger. For example:
one@localhost ~ $ cat duh.py
import pdb
class Coordinate(object):
pdb.set_trace()
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return "Coord: " + str(self.__dict__)
def add(a, b):
return Coordinate(a.x + b.x, a.y + b.y)
def sub(a, b):
return Coordinate(a.x - b.x, a.y - b.y)
one = Coordinate(100,200)
two = Coordinate(300,200)
add(one, two)
I want to see the values actually being used. Instead of seeing def __init__(self, x, y): I would like to see def __init__(self, 100, 200):
> /home/one/duh.py(14)()
-> one = Coordinate(100,200)
(Pdb) s
--Call--
> /home/one/duh.py(4)__init__()
-> def __init__(self, x, y):
(Pdb) s
> /home/one/duh.py(5)__init__()
-> self.x = x
(Pdb) s
> /home/one/duh.py(6)__init__()
-> self.y = y
(Pdb) s
--Return--
> /home/one/duh.py(6)__init__()->None
-> self.y = y
I'm totally not used being blind on what is going on inside of the interpreter and really would like to see what is going on in the internals like other scripting language debuggers (like JavaScript step-throughs).
解决方案
That pdb debugging doesn't look like fun. I can see why you don't like it.
Fortunately, there are some visual Python debuggers out there. The two that I use most often are commercial products, but they are both well worth the cost. They are Komodo IDE and IntelliJ IDEA. These are multi-language IDEs that support many other languages in addition to Python. There is also a Python-only version of IDEA called PyCharm.
There's also a great free option, Winpdb. It's easy to use: once you install and open it, use File/Launch and enter the full path to your .py file, and then you can start debugging.
Those products are all multiplatform, but if you're on Windows another free option is Microsoft's Python Tools for Visual Studio. You can install this either into the commercial Visual Studio 2015 or the free Community Edition of Visual Studio 2015.
To give you an idea, here are screenshots of your code from Winpdb, Komodo and IDEA. I stepped into the __init__ function in each:
Don't worry if you don't like the code font I used; that's just my personal setting. And of course in normal use the screen is not so cramped; I made it small to fit in a screenshot.
I highly recommend any of these visual debuggers - it's great to be able to step through your code with a single keystroke and watch the variables change as you go.