pythonのdebug

pdbを使用できる

import pdb

def factorial(n, sum=0):
	if n == 0:
		return sum

	pdb.set_trace()
	sum += n
	print(sum)
	return factorial(n-1, sum)

if __name__ == "__main__":
	factorial(5)
$ python3 test.py
> /home/vagrant/dev/test/test.py(8)factorial()
-> sum += n
(Pdb) bt
  /home/vagrant/dev/test/test.py(13)<module>()
-> factorial(5)
> /home/vagrant/dev/test/test.py(8)factorial()
-> sum += n
(Pdb) list
  3  	def factorial(n, sum=0):
  4  		if n == 0:
  5  			return sum
  6  	
  7  		pdb.set_trace()
  8  ->		sum += n
  9  		print(sum)
 10  		return factorial(n-1, sum)
 11  	
 12  	if __name__ == "__main__":
 13  		factorial(5)
(Pdb)