By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,536 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,536 IT Pros & Developers. It's quick & easy.

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

P: n/a
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/
--
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.