if __name__ == 'main':

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gtb

    #1

    if __name__ == 'main':

    Hi,

    I often see the following 'if' construct in python code. What does
    this idiom accomplish? What happens if this is not main? How did I get
    here if it is not main?

    Thanks,

    gtb

    =============== =============== =============== =

    if __name__ == 'main':
    myQuest('myQues t').Run()

  • Grant Edwards

    #2
    Re: if __name__ == 'main':

    On 2007-03-20, gtb <goodTweetieBir d@hotmail.comwr ote:
    I often see the following 'if' construct in python code. What does
    this idiom accomplish?
    It checks to see if the file is being run as the "main"
    program, and does something if that is so.
    What happens if this is not main?
    Nothing.
    How did I get here if it is not main?
    By importing the file.
    if __name__ == 'main':
    myQuest('myQues t').Run()
    >

    --
    Grant Edwards grante Yow! Thank god!!... It's
    at HENNY YOUNGMAN!!
    visi.com

    Comment

    • Facundo Batista

      #3
      Re: if __name__ == 'main':

      gtb wrote:

      I often see the following 'if' construct in python code. What does
      this idiom accomplish? What happens if this is not main? How did I get
      here if it is not main?
      ...
      if __name__ == 'main':
      myQuest('myQues t').Run()
      This idiom is for executing the code if you're running the .py directly,
      or doing nothing if you're just "import"ing the module.

      Take note, that when you import a module, __name__ gets the module name.

      Regards,

      --
      .. Facundo
      ..
      Blog: http://www.taniquetil.com.ar/plog/
      PyAr: http://www.python.org/ar/


      Comment

      • Patrick Down

        #4
        Re: if __name__ == 'main':

        On Mar 20, 11:49 am, "gtb" <goodTweetieB.. .@hotmail.comwr ote:
        Hi,
        >
        I often see the following 'if' construct in python code. What does
        this idiom accomplish? What happens if this is not main? How did I get
        here if it is not main?
        A quick example demonstrates the usage:

        C:\code>type temp.py


        print "Module name is",__name__

        if __name__ == "__main__":
        print "I was not imported"
        else:
        print "I was imported"

        C:\code>python temp.py
        Module name is __main__
        I was not imported

        C:\code>python
        Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
        on win32
        Type "help", "copyright" , "credits" or "license" for more information.
        >>import temp
        Module name is temp
        I was imported


        Comment

        • gtb

          #5
          Re: if __name__ == 'main':

          On Mar 20, 12:13 pm, "Patrick Down" <patrick.d...@g mail.comwrote:
          On Mar 20, 11:49 am, "gtb" <goodTweetieB.. .@hotmail.comwr ote:
          >
          Hi,
          >
          I often see the following 'if' construct in python code. What does
          this idiom accomplish? What happens if this is not main? How did I get
          here if it is not main?
          >
          A quick example demonstrates the usage:
          >
          C:\code>type temp.py
          >
          print "Module name is",__name__
          >
          if __name__ == "__main__":
          print "I was not imported"
          else:
          print "I was imported"
          >
          C:\code>python temp.py
          Module name is __main__
          I was not imported
          >
          C:\code>python
          Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
          on win32
          Type "help", "copyright" , "credits" or "license" for more information.>>i mport temp
          >
          Module name is temp
          I was imported

          Thanks, all! Makes great sense.


          Teas all 'round the canteen now,

          gtb

          Comment

          Working...