Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > if (__name__ == '__main__'): main(sys.argv[1:])

Reply
Thread Tools

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

 
 
Eli Stevens \(WG.c\)
Guest
Posts: n/a
 
      04-20-2004
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/
--


 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      04-20-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments