Should I start a Python program with:
if__name__ == \'__main__\':
some code...
And if so, why? I saw it many times but don\'t have a clue about
Guido Van Rossum suggests:
def main(argv=None):
if argv is None:
argv = sys.argv
...
if __name__ == "__main__":
sys.exit(main())
This way you can run main()
from somewhere else (supplying the arguments), and if you want to exit with an error code just return 1
from main()
, and it won't make an interactive interpreter exit by mistake.