Hi, just double checking I understand this correctly. Is the statement:
used to determine what a module does if its eitherCode:if __name__ == "__main__": ?
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?
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!
Cheers for the quick response Zeke, that cleared it up.
-Zip
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks