Defining Main Functions in Python: Overview
Many programming languages have a special function that is automatically executed when an operating system starts to run a program. This function is usually called main()
and must have a specific return type and arguments according to the language standard. On the other hand, the Python interpreter executes scripts starting at the top of the file, and there is no specific function that Python automatically executes.
Nevertheless, having a defined starting point for the execution of a program is useful for understanding how a program works. Python programmers have come up with several conventions to define this starting point.
By the end of this course, you’ll understand:
- What the special
__name__
variable is and how Python defines it - Why you would want to use a
main()
in Python - What conventions there are for defining
main()
in Python - What the best practices are for what code to put into your
main()
00:00 Hi, I’m Rich Bibby with realpython.com. Many programming languages have a special function that is automatically executed when an operating system starts to run a program.
00:10
This function is usually called main()
and must have a specific return type and arguments according to the language standard. On the other hand, the Python interpreter executes scripts starting at the top of the file, and there is no specific function that Python automatically executes.
00:28
Nevertheless, having a defined starting point for the execution of a program is useful for understanding how a program works. Python programmers have come up with several conventions to define this starting point. By the end of this tutorial, you’ll understand what this special __name__
variable is and how Python defines it, why you would want to use a main()
function in Python, what conventions there are for defining main()
in Python, and what the best practices are for what code to put into your main()
functions.
01:01
So, let’s get started. In some Python scripts, you may see a function definition and a conditional statement that looks like this. In this code, there is a function called main()
that prints the phrase "Hello World!"
when the Python interpreter executes it.
01:16
There is also a conditional, or if
statement, that checks the value of __name__
and compares it to the string "__main__"
. When the if
statement evaluates to True
, the Python interpreter executes main()
.
01:29 If you want to learn more about conditional statements, then you can find more tutorials on the Real Python site—just enter “conditional statements” in the search box.
01:39
This code pattern is quite common in Python files that you want to be executed as a script or imported into another module. To help understand how this code will execute, you should first understand how the Python interpreter sets __name__
, and that’s what’s coming up in the next lesson, where you will see that it depends on how the code is being executed.
Become a Member to join the conversation.