Ads

Python 5 - What does if __name__ == "__main__": do in Python?

 Hello All,

Good Morning, Good Afternoon, Good Evening Whatever zone you are in.

In this article we will we understand what __name__ == "__main__" will do in python programming.

In most of the program in python We see if __name__ == '__main__':

Basically It checks if a module is being imported or not.

__name__ 

is a global variable in Python which exists in all namespaces. It is typically the module's name (as a str type).

In General Word:

When Python code runs, it does two things:

  • First sets special variables like __name__ next
  • it executes all of the code found in the file.

Lets Create Simple Program Calculator.py

def my_calc():
    a = int(input("Enter a value : "))
    b = int(input("Enter b value : "))
    opt = int(input("Choose your option 1-Add 2-Substract 3-Multiplication : "))
    if opt == 1:
        Add(a,b)
    elif opt == 2:
        Sub(a,b)
    elif opt == 3:
        Mul(a,b)
    else:
        print('Invalid choice....')

def Add(a,b):
    print(f'Addition of a and b is : {a+b}')

def Sub(a,b):
    print(f'Subtraction of a and b is : {a-b}')

def Mul(a,b):
    print(f'Multiplication of a and b is : {a*b}')

if __name__=="__main__":
    my_calc()
    print(f"I am running Directly and value of __name__ is {__name__}")
else:
    print(f"I am running as import and value of __name__ is {__name__}")

We get below output:

PS D:\Python_Programming> & "C:/Program Files/Python310/python.exe" 
d:/Python_Programming/14_Calculator.py
Enter a value : 10
Enter b value : 20
Choose your option 1-Add 2-Substract 3-Multiplication : 2
Subtraction of a and b is : -10
I am running Directly and value of __name__ is __main__
PS D:\Python_Programming>

When we run the Calculator.py module (the source file) as the main program(like C:/Program Files/Python310/python.exe" d:/Python_Programming/14_Calculator.py), then interpreter will assign the hard-coded string "__main__" to the __name__ variable (i.e. __name__ = "__main__" )

That is the reason it prints the value I am running Directly and value of __name__ is __main__

Next, Lets create another program called MathOperation.py and we will import Calculator.py

import Calculator as c

c.my_calc()

Then output will be

PS D:\Python_Programming> & "C:/Program Files/Python310/python.exe" 
d:/Python_Programming/MathOperation
I am running as import and value of __name__ is Calculator
Enter a value : 25
Enter b value : 35
Choose your option 1-Add 2-Substract 3-Multiplication : 3
Multiplication of a and b is : 875
PS D:\Python_Programming> 

The interpreter will search for your Calculator.py file , and prior to executing that module, it will assign the name "Calculator" from the import statement to the __name__ variable, i.e. __name__ = "Calculator".

Due to this we will get the below message.

I am running as import and value of __name__ is Calculator

Thats it for this article, I hope you enjoyed. Please let us know incase any concern

See you in next article take care bye.

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !