Lab 3 - The main() function

In this section, the main() function is introduced. As a beginner you will spend more time reviewing other people’s code than writing your own. You will see the main() function used and wonder what it does. The main() function sets the entry point for a python program. A python program will run line by line, but it won’t run the functions until it comes to a line which calls the function.

Using our previous example this was called by the line translate_text() which calls the translate_text() function. You can review this below.


import boto3

client = boto3.client('translate')

def translate_text(): 
    response = client.translate_text(
        Text='I am learning to code in AWS', 
        SourceLanguageCode='en', 
        TargetLanguageCode='fr' 
    )
#### Add the new text below this line ####
    print(response) # this code is inside the function and will print the contents of the variable 'response' 

translate_text() # This line will call our function. Without it, python will not do anything.

When this code is run, the interpreter will define a special variable called __name__ and assign the value of "__main__" to the code in this python file. So the code in our python file becomes __name__ == "__main__".

When we use import statements, we can import code from other files into our python program. When this happens, the imported code is set a __name__ value of the modules name.

By setting the __name__=="__main__" we can control the order in which the code in this file is executed, telling python to run the code in this file which has the name of __main__ rather than the code imported from another file. This avoids situations where your code could run an imported script, resulting in unwanted behavior.

We do this using an if statement. If statements are covered in more detail later. At this stage, all you need to understand is that it is telling the python interpreter that if the __name__ is equal to __main__ which relates to the code in this python file, then run the main() function.

The main() function therefore sets the start point for our code to control the order in which our code executes. It is conventional to include all the calls to your functions within the main() function. This will help others to read your code and understand the logic and flow.

To implement this perform the following:

  • Create a new file called lab_3_main_function.py and add the following code.
import boto3

client = boto3.client('translate')

def translate_text():
    response = client.translate_text(
        Text='I am learning to code in AWS',
        SourceLanguageCode='en',
        TargetLanguageCode='fr'
    )
    print(response) # this code is inside the function and will print the contents of the variable 'response'

def main():
    translate_text()

if __name__=="__main__":
    main()
  • To run the program, enter the following command in the terminal:

python lab_3_main_function.py

What did we do?

  • We created a function called main() using def main():

  • We added the line translate_text() which calls the translate_text() function to the main() function

  • We added an if statement, which told the python interpreter that if the variable __name__ is equal to "__main__" then call the main function.

What did python do?

  • The python interpreter created a special variable called __name__ and assigned the code in this file a name of "__main__"

  • It then checked the if statement and if the __name__=="__main__" it called a function called main()

  • When it ran the main() function it found that it contained one call to another function called translate_text()

Well Done!

This topic can be confusing. If you have struggled to understand what is going on here, don’t worry! You will see this used throughout the rest of the workshop and you will soon start to recognize how to use it, you can then return to this lab to refresh your understanding.

In the next section, we will improve our program to remove hard coded variables and use arguments as parameters.

When you are ready, move onto the next section.