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

Discussion in 'Python' started by Eli Stevens \(WG.c\), Apr 20, 2004.

  1. I have a question about proper Python style when it comes to having a main
    function in a module. I'm fairly new to Python - a few months of
    very-part-time tinkering (lots'o'Java at work, shrug); my apologies if this
    has been hashed out before. Random Googling didn't enlighten me, so instead
    I'll ask here. :)

    Take the following strmod.py (it's silly, I know):
    ----
    import sys
    import getopt

    def main(sys_argv):
    try:
    opts, args = getopt.getopt(sys_argv, "clstu")

    for str_toPrint in args:
    for opt, arg in opts:
    if opt == '-c':
    str_toPrint = str_toPrint.capitalize()
    elif opt == '-l':
    str_toPrint = str_toPrint.lower()
    elif opt == '-s':
    str_toPrint = str_toPrint.swapcase()
    elif opt == '-t':
    str_toPrint = str_toPrint.title()
    elif opt == '-u':
    str_toPrint = str_toPrint.upper()

    print str_toPrint
    except getopt.GetoptError:
    pass


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

    Now, from what I have seen in terms of examples etc. would do something like
    (note the lack of sys_argv, and how sys.argv[1:] is imbedded in the
    getop.getopt call):

    ----
    import sys
    import getopt

    def main():
    try:
    opts, args = getopt.getopt(sys.argv[1:], "clstu")
    # ...
    print str_toPrint
    except getopt.GetoptError:
    pass


    if (__name__ == '__main__'):
    main()
    ----

    This essentially makes strmod.main() uncallable by anything else that needs
    command line args, right? The first pattern allows calls like
    strmod.main("-t -s pRINT mE tHE sAME uNTESTED".split()) from elsewhere. It
    seems to me that this would be very helpful when writing low-level utilities
    that could be driven by other higher-level utilities without needing to fall
    back to OS calls, etc.

    So... Is this a good idea? Bad idea? Is there a better way? I'm just
    trying to not fall into any newbie pit traps ("Hey, what's at the bottom of
    this nifty hole?" ;).

    TIA,
    Eli

    --
    Give a man some mud, and he plays for a day.
    Teach a man to mud, and he plays for a lifetime.
    WickedGrey.com uses SpamBayes on incoming email:
    http://spambayes.sourceforge.net/
    --
     
    Eli Stevens \(WG.c\), Apr 20, 2004
    #1
    1. Advertising

  2. Eli Stevens \(WG.c\)

    Peter Hansen Guest

    Eli Stevens (WG.c) wrote:

    > I have a question about proper Python style when it comes to having a main
    > function in a module. I'm fairly new to Python - a few months of
    > very-part-time tinkering (lots'o'Java at work, shrug); my apologies if this
    > has been hashed out before. Random Googling didn't enlighten me, so instead
    > I'll ask here. :)
    >
    > if (__name__ == '__main__'):
    > main(sys.argv[1:])

    vs.
    > if (__name__ == '__main__'):
    > main()


    As I think you suspected, "good style" is best determined in this
    case by testability. Go with the former and you aren't likely
    to regret it.

    -Peter
     
    Peter Hansen, Apr 20, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hal Styli
    Replies:
    14
    Views:
    1,697
    Old Wolf
    Jan 20, 2004
  2. Amy G
    Replies:
    4
    Views:
    385
    Amy G
    Feb 3, 2004
  3. Hakan Nilsson
    Replies:
    1
    Views:
    329
    Mirko Zeibig
    Feb 3, 2004
  4. =?iso-8859-1?Q?Fran=E7ois?= Pinard

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

    =?iso-8859-1?Q?Fran=E7ois?= Pinard, Apr 21, 2004, in forum: Python
    Replies:
    7
    Views:
    1,076
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Apr 23, 2004
  5. Ed Leafe

    Faking out __name__ == __main__

    Ed Leafe, Oct 31, 2004, in forum: Python
    Replies:
    5
    Views:
    565
    Bengt Richter
    Nov 1, 2004
Loading...

Share This Page