python calling python

François Pinard pinard at iro.umontreal.ca
Tue Aug 22 20:53:28 EDT 2000


[Alex Martelli]

> A fine touch might include:

> def main(args=sys.argv):
>     # whatever

I tried many styles for this, and finally decided that the most useful
idiom might be:

    def main(*arguments):
       import getopt
       options, arguments = getopt.getopt(...)
       for option, value in options:
           ...
       # rest of programs with arguments...

    ...

    if __name__ == '__main__':
        apply(main, sys.argv[1:])

so I use it almost identically in all my programs.  From another module,
one could just use:

    module.main(OPTION, OPTION..., ARGUMENT, ARGUMENT...)

passing strings for each.

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard




More information about the Python-list mailing list