A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://nedbatchelder.com/blog/200306/python_main_functions.html below:

Python main() functions | Ned Batchelder

Friday 6 June 2003This is more than 22 years old. Be careful.

A few weeks ago, Guido van Rossum wrote about Python main() functions in his weblog. There are good ideas in there, but I didn’t agree with them all. In particular, I thought this was odd:

def main(argv=None):
    if argv is None:
        argv = sys.argv
    # etc...

if __name__ == "__main__":


    sys.exit(main())

To me, it is better to keep the sys.exit and the sys.argv in one place:

def main(argv):
    # etc...

if __name__ == "__main__":


    sys.exit(main(sys.argv))

The weblog has an active comment system, with Guido responding thoughtfully. After I posted my thoughts, he agreed to “compromise” with me!

By the way: one trick I’ve used in main() functions: when converting numeric arguments from strings in argv, use eval(), not int(). That way, you can use any numeric calculations you want in your command line arguments.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4