Python: if __name__ == __main__: Explained

2 27
Avatar for RisingStar
2 years ago

To explain what

if name == 'main': does and used for

Ans: you can think of it as a simple "if Codition" which is saying that when

value of "name" is "main" then

do something (something could be anything you wrote in the body of if).

 

In other words, everything you write in this if's body, will only be executed when the value of "name" will be "main".

now that we know what it used for let's see when it comes in handy...

suppose I was doing something and I had to print something say 3 variables, I simply printed them using print()

 a=10

 b=10

 c=10

 print( a,b,c)

then, I realize I need a sum function to add 3 numbers, so I declared the function

def sum(num1,num2,num3):

 return num1+num2+num3

after declaring, I tested it's output

print("testing=", sum(5,10,15))

it's output (30) verifies that it is working fine. Good for me, I start my work again.

then, all of sudden, I feel the need of another file (which I named "newfile.py") and now I was working in it.

while working I again felt that I need to add a few numbers, and, I started to think of possible solutions to that

 

I came up with 3 solutions to that

1. Copy paste the above sum function to that file

2. Create a new function for that file

3. import the function by importing this file to that one

 

discussion:

1. Here, the first solution is the simplest, but I can't pick that one because you all will agree that it isn't the good practice. Right?

2. Moving to the second one, it won't do me anything good either because it just Denys " Reuse Guru's teachings".

3. Going through the solutions one by one leaves me with the third one "import this file to that file and use the function.

Seems good enough. Right? So I pick this one. And import this file to "newfile.py".

 

 

Now, that I have imported the file I use it's function by referencing it to file name, In other words, instead of just calling "sum(5,5,5)" I have to call "whyifmain.sum(5,5,5)" for it to work. Fair Enough, You have to give credits to the owner. Right!

 

 

then I ran it and even though the output was right; it was messed up.

So, what do you think happened? Why is there so much content in the output.

Ans: The reason for that is "import". See, the point is that when you use import to use a function or something from a file, it gives you the function along with all the content in it, Hence the output.

 

So, now, we know the reason why it happened, but the question is, how do we fix that? 

here, again I tried to think of possible solution and the solutions were:

1. Just copy paste the function

2. Make your own

3. Use if name == 'main':

 

discussion:

Skipping the first two (you know why) I simply jumped to the third one

3. Use if name == 'main':

but, the question is, how is it gonna help me?

Ans: Remember, what I told you above, how it works? It does whatever is told in it's body if the value of "name" is "main" or in other words, when you are in the main file.

you can even verify it by printing "print("name of the file is", name)"

 

see the file whyifmain.py has name "main" ...let's check out the "newfile.py"

see the name of the file in newfile.py isn't main, which means the condition if name == main: is going to be false, and hence it won't perform the task given within it's body.

 

_________________________________________________

Now, let's just apply this knowledge to practice and put everything in the file "whyifmain.py" except for the function sum() in the if name == main: body.

 

def sum(num1,num2,num3):

 return num1+num2+num3

if name == 'main':

a = 10

b = 10

c = 10

print(a, b, c)

print("testing=", sum(5, 10, 15))

Now if I run this file, it will give the same output in whyifmain.py

but when I run the "newfile.py" it will also work as I want it do be. let's try

 

 

see the file newfile.py was only able to use the function which was outside

if name == 'main': body.

So, that was the working and usage of if __name__ == '__main__':

I hope you got it. Thanks for being here, will see you next time.

Sponsors of RisingStar
empty
empty
empty

Here is a question for you

Question: What do you think would have had happened if I had put the function in the if's body as well?

2
$ 0.01
$ 0.01 from @LeonaReed
Sponsors of RisingStar
empty
empty
empty
Avatar for RisingStar
2 years ago

Comments

Very interesting and wonderful entry to read family... Welcome my friend

$ 0.00
2 years ago

Thank you so much

$ 0.00
2 years ago