Monday, 25 September 2017

The concept of if _ _name_ _ = = '_ _main_ _ condition

The concept of      if _ _name_ _ = =  '_ _main_ _ ' :

Example:  addition.py

def  add(x,y):

     z=x+y

     print(z)


add(4,5)

Output:

9

Example: adding.py

from addition import add


add(2,3)

Output:

9

5

Note:

  • here executing both main module and sub module add function.
  • in these cases we don't want execute imported module add function(sub module add function). 
  • i want execute only main module add function.
in this situation we are using   if  _ _name_ _ = = '_ _main_ _' :


Example:


def add(x,y):


    z=x+y

    print(z)

if __name__= = '__main__':

    add(4,5)

Example:

from addition import add


add(2,3)


Output:

5


No comments:

Post a Comment