I used b "classname:function" or "b classname::function", and those didn't work. now I use "b linenum" as a workaround.but as I modifiy my code frequently, linenum changes.So how to make a breakpoint on class member function in python?I google it && read the python manual, and there's no direct answer.thanks!
In pdb
, the Python debugger, the breakpoint can be set with
b classname.methodname
after the class definition has been parsed.
For example,
% pdb ~/pybin/test.py
> /home/unutbu/pybin/test.py(4)<module>()
-> class Foo(object):
(Pdb) l
1 #!/usr/bin/env python
2 # coding: utf-8
3
4 -> class Foo(object):
5 def bar(self): pass
6
7 foo=Foo()
8 foo.bar()
[EOF]
Setting the breakpoint before parsing the class fails:
(Pdb) b Foo.bar
*** The specified object 'Foo.bar' is not a function
or was not found along sys.path.
but after parsing the class:
(Pdb) n
> /home/unutbu/pybin/test.py(7)<module>()
-> foo=Foo()
(Pdb) l
2 # coding: utf-8
3
4 class Foo(object):
5 def bar(self): pass
6
7 -> foo=Foo()
8 foo.bar()
[EOF]
setting the breakpoint works:
(Pdb) b Foo.bar
Breakpoint 1 at /home/unutbu/pybin/test.py:5
(Pdb)
(Pdb) r
> /home/unutbu/pybin/test.py(5)bar()
-> def bar(self): pass
For a persistent break point, on the line before you want to break, you can also use:
import pdb; pdb.set_trace()
See the python doc for more detail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With