What is the main() Function in Python?
The starting point for a program in most programming languages is a particular function called main(). On the other hand, the python interpreter reads each line sequentially from the beginning of the file and lacks a main() function.
In python, we can also set the starting point of the code snippet using the main() function and __name__ property. While compiling the code, the python interpreter checks for some certain special variable, and __name__ is one such special variable, so for calling the main() function as the starting point of the program, __name__ is set to __main__.
A Basic Python main()
For using the main() function in Python code, we can check a condition where __name__ == "__main__" and pass the main () function in the statement of conditional If. So, the code inside the main() function will be the start of the code.
Example:
Below is the small example of main() function in Python:
Output:
It can be seen that the main() function is executed first
Main function as Module
When a Python script is imported as a module, the__name__variable now has the same value as the name of the Python script imported. So main() function in Python can be used as a module
Example:
Let's take a file namedfile1.py and file2.py and will import file1 in file2.
File1.py:
File2.py: Importing file1 in file2
Output:
After importing file1, it is used as a module and is executed.
Using if Conditional with __name__
we can use the condition if __name__ == "__main__" and conditionally execute the given commands when__name__equals "__main__". Let's take an example of the following:
Example:
In this example, we execute the conditional and the given statements.
Output:
Best Practices for Python Main Functions
These best practices can be used with the main() function in Python for making the code more interactive and readable:
- Put the majority of your code into a function or a class.
- To control the execution of your code, use __name__.
- Create a main() function to store the code that you want to run.
- Use the main function to call additional functions.
Examples
Create a Function Called main() to Contain the Code You Want to Run
We can use the main() function in Python and call it wherever we want in our code, we can check a condition where __name__ == "__main__" and pass the main the main() function in the statement of conditional If. So, the code inside the main() function will be the start of the code.
Example:
Below is a small example of the following:
Output:
It can be seen that the main() function is executed.
Call Other Functions From main()
We can call more functions from the main() function in Python and then use those values for our purposes.
Example:
we are getting values of a and b from funa() and funb() functions.
Output:
Conclusion
- The starting point for a program is called the main() function.
- In python, we can also set the starting point of the code snippet using the main() function and __name__ property.
- When a Python script is imported as a module, the __name__ variable now has the same value as the name of the Python script imported.
- These best practices can be followed for making the code more interactive and readable:
- Put the majority of your code into a function or a class.
- To control the execution of your code, use __name__.
- Create a main() function to store the code that you want to run.
- Use the main function to call additional functions.