- Main function is the entry point for most of the programming languages like C, C++, Java, C# etc.,
- Python is a Interpreter based programming language and the interpreter executes the code sequentially.
- Python comes with a number of special variables and methods whose name is preceeded and followed by
__
, and the interpreter sets its values during execution, one of them is__name__
whose value is set as__main__
- For Python main function we need to define a function and then use
if __name__=='__main__'
condition to execute the defined function - Its not mandatory to have a main function in Python like other programming languages, but its a good practise to use it.
mainexample.py
print("Good Morning, Welcome to Python Main Code")
def display():
print("Hello Display")
def main():
print("Hello Main Function")
if __name__=="__main__":
main()
print("__name__ value:",__name__)
print("Good Evening")
>>> import mainexample
Good Morning, Welcome to Python Main Code
__name__ value: mainexample
Good Evening
- If the python source file is imported as module, python interpreter sets the
__name__
value to module name, so the if condition will return false and main method will not be executed. - Notice that first print statement and the last two print statements are getting printed from
mainexample.py
source file. Notice the value of__name__
is different and hence main method is not executed.
print("Good Morning")
def display():
print("Hello Display")
if __name__=="__main__":
main()
def main():
print("Hello Main")
print("Good Evening")
print("__name__ value: ",__name__)
C:\Users\Girish>python mainexample.py
Good Morning
Traceback (most recent call last):
File "mainexample.py", line 8, in <module>
main()
NameError: name 'main' is not defined
- Notice in the above code, the python program statements are executed line by line, so it’s important to define the
main()
method first before the if condition to executemain()
method. Else you will get error asNameError: name 'main' is not defined.
- When you include if
__name__ == “__main__”
in the program, it tells the interpreter that it should always be executed as a standalone program only, and you cannot execute this program if it is imported as a module. It gives error as shown in above code.
Now, let us write few functions and call those functions from main() as below
maindemo.py
import math
print("Welcome to Python Main Demo")
def display():
print("Hello this is Display() function")
def squareroot(num):
print("squareroot function code starting")
print("Square Root of: " + str(num) + " is " + str(math.sqrt(num)))
print("squareroot function code last line")
def main():
print("Start of Main Block")
display()
squareroot(64)
print("End of Main Block")
if __name__=="__main__":
main()
print("Last line of Source file")
C:\Users\Girish>python maindemo.py
Welcome to Python Main Demo
Start of Main Block
Hello this is Display() function
squareroot function code starting
Square Root of: 64 is 8.0
squareroot function code last line
End of Main Block
Last line of Source file
References
- https://docs.python.org/3/library/main.html
- https://docs.python.org/3/tutorial/modules.html
Learn more about Python features in our upcoming blog articles
Happy Learning!