Published on 2020-11-06, by Javed Shaikh
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.
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.
Lets try some examples to know it better.
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
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__
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()
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
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
test2 function,value of __name__: test2
test3 function,value of __name__: __main__
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 🙂
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.
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.
We are going to build a CLI app using Python to change desktop wallpaper every given number of minutes. Every wallpaper downloaded from internet will be unique and our app will change the wallpaper based on the time we set.
There are many ways to monitor and set alerts for your web server using third party apps but they don't come with free, be it Pingdom or Amazon's CloudWatch. In this post I am going to show how we can write and setup our own alert tool with just few lines of code
In this post we are going to see how we can use Python to find the size of any file and folder. We will check how many files we have inside a folder and how many of them are empty files and how to delete those empty files.