Process flow in Python?

Ref: My remote camera robot project.

I am uploading the listing of the original remote_robot.py as a PDF, printed with line numbers, so that I can reference individual parts of the listing.  If the forum won’t let me upload as a PDF, I will append “txt” to it.

I am not in-lining the code as it is long and would be a pain to read within the context of this posting.

{N.B. PDF’s, and such-like should be white-listed so that we can upload them for reference.)

remote_robot.pdf.txt (50.5 KB)

Note that the listing appears to be, in essence, divided into three major sections, though they are not delineated as such:

Section 1: Lines 1 to 63
This section contains no self-defined classes, though it may reference classes defined outside the Python script for initialization purposes.  It appears to do everything necessary to allow the rest of the program to work - in essence initialization code, equivalent to the “at start” section in an Arduino script.

Section 2: lines 64 to 207
This section contains class definitions used elsewhere in the program.  Many of these classes appear to exist to “kick-off” other processes that run as separate processes/threads in the background when started and have an independent existence.

Section 3: lines 208 to end.
This is the "if name == “main” " section.
This part of the program appears to be the “main” process that kicks off the rest of the program, roughly equivalent to the “forever” section in an Arduino script.

Note that global variables declared in the first section do not reach here - as this appears to be a “shell” that exists only to activate and use the classes and functions defined elsewhere.

====================================

What appears to happen is that the Python script begins at the top of the script and proceeds as far as the first class definition, then skips all the way to the last section, perhaps after reading and parsing the class definitions.  Or, maybe, the entire program is read and parsed, and then execution begins at the top.

What defines the execution order of a Python script?

Is this analysis of the program flow correct?  If so, why?  Why isn’t the initialization code placed in the “main” section, as that appears to be the actual beginning of the program, with classes and overall global variables/constants defined first?