Mainline Logic

This next topic covers mainline logic which is extremely important, especially if you are using modules. 

Without functions, we have seen that code will execute as soon as the program is loaded.  Functions help to control when code is executed.

When you import a module into a program, the module's code also needs to be encased in functions – otherwise the module's program will execute as soon as you import it.  Just like with the math module, you can call the individual functions contained within the module.

Since you can create a module out of any piece of code you write in Python, you also should encase your programs completely in functions.  This is a way of controlling the mainline logic of the program.

Here is an example:

Code Output
def main():
    print("Hello!")
    secret_code()
    print("Bye!")

def secret_code():
    print("Shh! It's a secret!")

main()
Hello!
Shh! It's a secret!
Bye!

In this example, the main logic for the program is enclosed in the main function.  This function performs actions, and calls a second function called secret_code.

Launch Exercise

"But wait!" you say.  By calling the main function (the last line), the mainline logic still executes if it is imported into another program.  This is true.  We need a way to automatically call the main function if we are running this module by itself, but to NOT call the main function if this code has been imported into another program.  Here's how we do it:

Code Output
def main():
    print("Hello!")
    secret_code()
    print("Bye!")

def secret_code():
    print("Shh! It's a secret!")

if __name__ == "__main__":
    main()
Hello!
Shh! It's a secret!
Bye!

The __name__ variable is built-into Python.  It tells the scope of the code which is being executed.  If the code is running at the top level of the program, then __name__ is equal to __main__.  If the code has been imported into the program, then the __name__is equal to the name of the module.  This allows us to automatically execute main() when we are running this code by itself – and when we are importing this code into another program, it will only execute the main function when we explicitly call it.

Moving forward as a best practice for all of your future programs:

  • You should define a main function
  • You should use the if __name__ == "__main__": conditional to execute the main function

Debugging

If you are using a text editor to write your scripts, you might run into problems with spaces and tabs. The best way to avoid these problems is to use spaces exclusively (no tabs). Most text editors that know about Python do this by default, but some don’t.

Tabs and spaces are usually invisible, which makes them hard to debug, so try to find an editor that manages indentation for you.

Also, don’t forget to save your program before you run it. Some development environments do this automatically, but some don’t. In that case, the program you are looking at in the text editor is not the same as the program you are running.

Debugging can take a long time if you keep running the same incorrect program over and over!

Make sure that the code you are looking at is the code you are running. If you’re not sure, put something like print('hello') at the beginning of the program and run it again. If you don’t see hello, you’re not running the right program!