[파이썬] pdb `list` 또는 `l` 명령어로 소스코드 확인

To use the list or l command in pdb, follow the steps below:

  1. Start by importing the pdb module at the beginning of your Python script:
import pdb
  1. Next, set a breakpoint in your code where you want to start debugging. This can be done by adding the following line:
pdb.set_trace()
  1. Run your program and wait for it to reach the breakpoint. Once the breakpoint is hit, you will see a (Pdb) prompt in your console.

  2. To view the source code, you can use either the list or l command:

(Pdb) list
(Pdb) list 10, 20
(Pdb) list 15
  1. The source code will be displayed in your console, and the line currently being executed will be marked with an arrow (->). You can navigate through the code by using the l command with different arguments or pressing Enter to repeat the last list command.

Using the pdb list or l command can greatly assist you in understanding the flow of your program and identifying any errors or unexpected behavior. It allows you to examine the source code while interacting with the program during debugging sessions.

Remember to remove or comment out the pdb.set_trace() line once you have finished debugging, as it will pause the program execution every time it is reached.