Skip to content

Main


A Program written in languages of C family (C, C++, Java, C# etc.) needs the main() function to indicate the starting point of execution. main() In Python, on the other hand, there is no concept of the main() function, as it is an interpreter based language and can be equally used in an interactive shell. The Python program file with .py extension contains multiple statements. The execution of the Python program file starts from the first statement. main()interactive shell.py Python includes the special variable called name that contains the scope of the code being executed as a string. main is the name of the top-level scope in which top-level code executes. __name__``__main__ For example, the scope of the code executed in the interpreter shell will be main, as shown below. __main__

>>>__name__
'__main__'

>>>__name__ '__main__' All the functions and modules will be executed in the top-level scope main_ in the interpreter shell. __main___

>>> def f1():
print(__name__)
>>> f1()

`>>> def f1(): print(name)

f1()Even the inner functions are executed in the top-level scope __main__:main`

>>> def f1():
print(__name__)
def f2():
print(__name__)
f2()
>>> f1()
__main__
__main__

`>>> def f1(): print(name) def f2(): print(name) f2()

f1() main mainA Python file can contain multiple functions and statements that can be executed independently. For example, consider the following addition.py:addition.py`

def add(x,y):
z=x+y
print('add() executed under the scope: ', __name__)
return z
x=input('Enter the first number to add: ')
y=input('Enter the secode number to add: ')
result = add(int(x),int(y))
print(x, '+', y,'=', result)
print('Code executed under the scope: ', __name__)

`def add(x,y): z=x+y print(‘add() executed under the scope: ’, name) return z

x=input(‘Enter the first number to add: ’) y=input(‘Enter the secode number to add: ’) result = add(int(x),int(y)) print(x, ’+’, y,’=’, result) print(‘Code executed under the scope: ’, name)` Python program file can be executed in the following ways:

  1. Use the command prompt/terminal to execute the Python file as a script.
  2. Import Python code from one file to another using the import statement

As you can see, the addition.py executed under the top-level scope main. addition.py``__main__ The addition.py file can be used as a module in another file or in interactive shell by importing it. addition.py Let’s see what happens when you import the addition module in the interactive shell. addition

>>> import addition
Enter the first number to add: 3
Enter the secode number to add: 3
add() executed under the scope: addition
3 + 3 = 6
Code executed under the scope: addition

>>> import addition Enter the first number to add: 3 Enter the secode number to add: 3 add() executed under the scope: addition 3 + 3 = 6 Code executed under the scope: addition Above, the import statement starts executing from the first statement. But, we only want to use the add() method and don’t want to execute the other statements. add() Here we can use the special variable name to check the scope and execute the statements of the addition.py file only when it executes from the command prompt/terminal independently but not when imported it in some other file/module. Rewrite the addition.py, as shown below. __name__``addition.py``addition.py

def add(x, y):
z=x+y
print('add() executed under the scope: ', __name__)
return z
if __name__ == '__main__':
x=input('Enter the first number to add: ')
y=input('Enter the secode number to add: ')
result = add(int(x),int(y))
print(x, '+', y,'=', result)
print('Code executed under the scope: ', __name__)

`def add(x, y): z=x+y print(‘add() executed under the scope: ’, name) return z

if name == ‘main’: x=input(‘Enter the first number to add: ’) y=input(‘Enter the secode number to add: ’) result = add(int(x),int(y)) print(x, ’+’, y,’=’, result) print(‘Code executed under the scope: ’, name)Above, the if condition check that if the scope is __main__ then only execute the code that takes user's inputs and adds them.mainNow, let's see what happens when we import the above addition module in the interactive shell.addition`

>>> import addition
>>> addition.add(3,3)
add() executed under the scope: addition
6

`>>> import addition

addition.add(3,3) add() executed under the scope: addition 6You can also use the from import statement, as shown below:from import`

>>> from addition import add
>>> add(3,3)
add() executed under the scope: addition
6

`>>> from addition import add

add(3,3) add() executed under the scope: addition 6As you can see, because we used an if condition to check the scope, it does not execute user input codes after importing the addition module, because it executes under the module's scope, which is addition scope. It only imports the add() method. The same thing will happen when you import the addition module in other modules.additionadditionadd()“addition` Now, let’s see what happens when you execute it from the command prompt/terminal.

As you can see, it still executes the same code because of addition.py being executed in the top-level scope main. addition.py``__main__ Thus, value of the name allows the Python interpreter to determine whether a module is intended to be an executable script or not. If its value is main, the statements outside function definitions will be executed. If not, the contents of the module are populated in top-level module (or interpreter namespace) without the executable part. name``main Note: The Python script file executing from the command prompt/terminal will be executed under the top-level scope main scope. However, importing a module will be executed under the module’s own scope. So, the top-level scope will be main, and the second scope would be module’s scope. __main__``__main__ Thus, using the special variable name and the top-level scope main increases the reusability. The Python script file can be executed from the command prompt/termainal as an indipendent script as well as when imported as a module. __name__``__main__