Python Forum
Why do we use __main__ when we can call a function directly?
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why do we use __main__ when we can call a function directly?
#1
Hi Everyone,

I am a newbie to Python. As I was learning Functions, I am not able to understand the difference between calling the function directly and calling it under __main__. Is there any advantage to call a function under main? Is it something important like main function in Java?
SomeFn() also gets executed directly and funct() also get executed directly.

My sample code:
def funct():
    print("Value of __name__ is: ", __name__)
    print('This statement is in main function')

def someFn():
    print ('This function is not called under the main function')
someFn()

if __name__ == "__main__":
    funct()
Say for e.g., I have 4 functions fn1, fn2, fn3, fn4 - Will there be any difference if I call them by sequence under main or outside main?
Reply
#2
People use __name__ to determine if the script is being imported or not. If it's equal to "__main__" then it's not an import. The idea being, you want to run your main method if and only if you're not being imported.

Java knows to call your main method. Python has no such similarity. When you run or import a script, the contents are executed. So it's up to the script to determine whether it should run its main function or not, rather than being built into the language.
Reply
#3
just to add to micseydel's answer. Given your sample code
if you execute your script it will execute both functions (because of line 7 and line 9-10)
if you import it in another script it will execute someFn (because of line7) but will not execute the other one
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Thanks to both for the quick response.

Does this mean that if I am not planning to import, then I can just directly call the functions and not put them under main. Apologies for my ignorance, but since I am not able to see the difference or understand which is better of the two, I am asking it again.
Reply
#5
yes, you can just call them.
As to which is better - in my opinion it's better to get the habit of using if __name__ =='__main__':
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Thanks a ton.. Now it is clear..
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  __name__ and __main__ in functions Mark17 3 735 Oct-12-2023, 01:55 AM
Last Post: deanhystad
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 794 May-02-2023, 08:40 AM
Last Post: Gribouillis
  Import XML file directly into Excel spreadsheet demdej 0 847 Jan-24-2023, 02:48 PM
Last Post: demdej
  question about if __name__=="__main__" aaander 6 1,358 Nov-13-2022, 09:06 PM
Last Post: deanhystad
  if __name__=='__main__' albin2005 3 2,148 Sep-07-2021, 09:21 AM
Last Post: albin2005
  how to call an object in another function in Maya bstout 0 2,081 Apr-05-2021, 07:12 PM
Last Post: bstout
  In this function y initially has no value, but a call to foo() gives no error. Why? Pedroski55 8 3,500 Dec-19-2020, 07:30 AM
Last Post: ndc85430
  Struggling for the past hour to define function and call it back godlyredwall 2 2,221 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  list call problem in generator function using iteration and recursive calls postta 1 1,908 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  function call at defined system time? Holon 5 3,232 Oct-06-2020, 03:58 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020