0

I am looking at a body of code and am trying to learn how to tell the order that each line executes. Is there a way to get this from a command in Python? Or is there a way to find out the order that Python executes code?

  • 1
    Any debugger will have single-step mode. See this lovely reference for [debugging help](https://ericlippert.com/2014/03/05/how-to-debug-small-programs). – Prune Jun 13 '21 at 23:13

1 Answers1

0

You can step through the program to see which line is being executed when. It's not really possible to say "this line executes after this one" because the code branches every time there is a conditional statement and the execution order changes.

You can step through the code to see which order the program executes lines for one specific set of conditions, though.

See https://www.jetbrains.com/help/pycharm/stepping-through-the-program.html

It is worth noting that generally the code executes from top to bottom, unless its told to go somewhere specifically by a line, or it is returning to the start of a loop etc.

In many programs, after the imports and any initial setup, the code itself "starts" from the section that says

if __name__ == "__main__"

You can find an explanation of why here: What does if __name__ == "__main__": do?

yuuuu
  • 738
  • 3
  • 11