Saturday, July 24, 2010

python: if __name__ == "__main__"

This is a simple "trick" so that python files can be used as both importable/reusable modules and also as standalone python programs.

From another perspective, this answers the common interview question,
"What considerations do you take while writing a python module? Or do you just write a module like a regular python program and then import it blindly? Don't you have to take some extra care/precautions while writing a python module?"


See this link for a simple and illustrative example:
http://pyfaq.infogami.com/tutor-what-is-if-name-main-for

No comments: