Main Method
Python has special "dunder" methods. They are referred to as "dunder" because they use double-underscore characters. In this chapter, you will learn to use "__main__" dunder method to control how your programs flow.
Learning Objectives
You should be able to:
- Explain the purpose of using
__main__to organize code - Write a main dunder (double-under) method to control the flow of your program
Video Walkthrough
Use this video to follow along with the steps in this lab.
Main Functions
"Main" functions are the entry point to your application. In previous chapters, all of the code simply ran from top to bottom. Variables were defined, functions were defined, and functions were called. As programs become more complex, it becomes important to control the flow of the application. A "main" function tells Python where the program should start.
- Create a file called
program.pywith the following code.
def greet(name):
print("Hello " + name)
greet("Eve")
if __name__=="__main__":
greet("Alice")
greet("Bob")
new_name = input("Give me another name: ")
greet(new_name)
- Run the program using the following command.
python program.py
- Enter a name when prompted. The output should be:
Hello Eve
Hello Alice
Hello Bob
Give me another name: Doug
Hello Doug
(Of course, if you used a name other than "Doug," that name would appear in your output.)
- While in the same working directory as
program.py, launch the Python interpreter. - Import "program.py" file with the following code. Importing loads the file and makes the functions and variables defined in the file available for use.
import program
Notice the output.
Python 3.12.2
Type "help", "copyright", "credits" or "license" for more information.
>>> import program
Hello Eve
- None of the code in the
__main__section was executed. This is because the program.py file was not executed directly from the terminal. But, we will not have access to the "greet" function defined in program.py. - Run the following code in the interactive Python shell:
program.greet("Jen")
-
You will see "Hello Jen" as a result. In this case, Python knew that when you ran "import program" you wanted to run the code inside program.py.
-
As programs become more complex, you will start to include code from several .py files. Code in the
__main__method will only be run if it is called from the terminal, e.g.,python program.py.
Challenge
- Create a small Python program.
- Define at least one variable.
- Define at least one function.
- Create a
__main__method that calls the function and uses the variable defined earlier in the program.
Reflection
- Why would it be useful to separate code into different files?
Key Terms
- Dunder Methods: Short for "double underscore methods," these are special methods in Python that have double underscores before and after their names (e.g.,
__init__,__str__). Dunder methods are also known as magic methods or special methods. They enable the customization of the behavior of Python objects and allow the implementation of operator overloading, object initialization, and other advanced features. For example,__init__is used to initialize an object's attributes, and__str__defines the string representation of an object. - Main Function: The entry point of a program when it is executed as the main program.