Most of the high level languages such as Java,C/C++ have special main method defined as the execution entry point. This tells interpreter where to start the execution of codes. Its like an entry point of a program. If you don't define a main method in such languages, your code is never going to execute. But in case of Python, its totally different. In Python, interpreter executes codes from top to bottom by default irrespective of main function.
In this post we are going to try different cases with some examples to understand the concept of main function in Python.
Is there a main function in python
Yes there can be a main function in Python but interpreter doesn't care if it is present or not. Interpreter will execute codes right from the start of the program.
Python has a special variable called __name__ that will tell the name of the program or function currently being executed.
Two things to note about __name__
- Python sets this variable to __main__ when the same file gets executed as the main program.
- When Python interpreter imports codes or programs from another file, the interpreter sets this variable to the name of that module or file.
Lets try some examples to know it better.
CASE 1: main function in a Python script
Lets create a new file and name it as test1.py and just put this one line and execute.
#test1.py
print('Hello from test1, value of __name__ is',__name__)
OUTPUT: Hello from test1, value of __name__ is __main__
As shown when we run this file, it prints the value __main__ because this is the main program which is being executed directly and not being imported
CASE 2:
If we run below program, output will still be similar
#test1.py
print('in Test1, __name__:',__name__)
if __name__ == "__main__":
print("hello from ",__name__)
OUTPUT: in Test2,__name__:__main__ hello from __main__
CASE 3: define a main function or entry segment
Since Python runs codes from start to end, its always a good practice to define a entry point or function.
As shown below, test2 is the main function for this program
#test2.py
def test2():
print('test2 function,value of __name__:',__name__)
if __name__ == "__main__":
test2()
OUTPUT:
test2 function,value of __name__: __main__
Here, Interpreter will call function test2() only when its executing main function else it will not call this function even though interpreter will start execution from top. If we run this code as a main program without importing to another file, this will print __main__ as shown above
CASE 4: main function when importing another file
As we mention before if Python interpreter imports codes from another file, the interpreter sets this variable to the name of that module or file.
Lets check it out.
#test3.py
from test2 import *
def test3():
test2()
print('test3 function,value of __name__:',__name__)
if __name__ == "__main__":
test3()
Guess the output. We are calling test3() if this is a main program and inside test3() we are calling test2() function imported from another file test2.py
OUTPUT:
test2 function,value of __name__: test2
test3 function,value of __name__: __main__
Things to note again
When we ran test2.py in CASE 3 example, __name__ was __main__ but when we ran same file imported into test3.py, the __name__ became the name of the module or file name ๐
Best Practice
Since we saw how in Python, codes are executed from first line, its always a good practice to define a main function and call all other functions and code blocks from there. See the example below
#test4.py
from test2 import *
def test3():
test2()
print('test3 function,value of __name__:',__name__)
def main():
print('this is main function')
test3()
if __name__ == "__main__":
main()
In above example, our code will start execution from main() function and will call others from inside it.
Conclusion:
Its always important to know entry point to your program. Use of __name__ can be helpful to identify in which program you are in. Since in Python code execution starts from first line of the program, its always a good practice to define a function and put all your changes in the function and call it based on __name__ variable.
Thanks for reading. Let me know your thoughts in the comment section.
My other recent posts
- How to get email alert when your website is down using shell and Python? https://shaikhu.com/how-to-get-email-alert-when-your-website-is-down-using-shell-and-python
- How to change desktop wallpaper every minute using Python Change desktop wallpaper
- Files, Folders and Python Files,folders and Python
- How to keep your computer awake Keep computer awake
- How to monitor CPU utilization in Linux cpu utilization
- How to schedule jobs using crontab How to use crontab