Closed Thread
Results 1 to 3 of 3

Thread: if __name__ == "__main__" ?

  1. #1
    ZipOnTrousers is offline Learning Programmer
    Join Date
    Nov 2008
    Posts
    94
    Rep Power
    0

    if __name__ == "__main__" ?

    Hi, just double checking I understand this correctly. Is the statement:

    Code:
    if __name__ == "__main__": ?
    used to determine what a module does if its either

    a) a standalone program and
    b) an imported module?

    For example, if __name__ != __main__, then __name__ is its filename and the module is imported, meaning the body of the if statement is carried out instead of the rest of the module?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: if __name__ == "__main__" ?

    That's intended to determine if the python file is being ran directly or not. If it is, then it will be named __main__, if it's not (IE it was imported from elsewhere) it will be given a different name. That is a common Python idiom to ensure code that would otherwise create objects and execute code on it's own doesn't when it's imported by other Python files. (You usually want to keep test code there in case you make changes during development)
    Wow I changed my sig!

  4. #3
    ZipOnTrousers is offline Learning Programmer
    Join Date
    Nov 2008
    Posts
    94
    Rep Power
    0

    Re: if __name__ == "__main__" ?

    Cheers for the quick response Zeke, that cleared it up.

    -Zip

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 8
    Last Post: 09-20-2011, 05:11 AM
  2. MASM How To Use "Open" And "Save As" (Win32 API) ?
    By RhetoricalRuvim in forum Assembly
    Replies: 12
    Last Post: 08-13-2011, 04:10 PM
  3. "\r\n" are for some reason turned into "\r\n\r\n"; what's wrong?
    By RhetoricalRuvim in forum Ruby Programming
    Replies: 1
    Last Post: 08-06-2011, 05:55 PM
  4. Alex G. CSS "Sexy Buttons" - what about "submit on 'ENTER'" ?
    By shackrock in forum JavaScript and CSS
    Replies: 7
    Last Post: 12-05-2010, 04:57 PM
  5. Replies: 2
    Last Post: 11-01-2010, 11:52 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts