Request for feedback on my first Python program

Aahz aahz at pythoncraft.com
Fri May 30 13:37:33 EDT 2003


In article <3ED7890A.D9328D30 at engcorp.com>,
Peter Hansen  <peter at engcorp.com> wrote:
>Aahz wrote:
>> In article <slrnbdeevn.694.andrew-usenet at athena.jcn.srcf.net>,
>> Andrew Walkingshaw  <andrew-usenet at lexical.org.uk> wrote:
>>>
>>>def main():
>>>    if len(sys.argv) != 2:
>>>        usage(sys.argv[0])
>>>    f = open(sys.argv[1], "r")
>> 
>> This is generally a Bad Idea; your functions should be generic and
>> sys.argv handling should be done under "if __name__=='__main__':".
>
>I don't entirely agree.  Although I can see calling main as 
>
>   if __name__ == '__main__':
>       main(sys.argv[1:]) 
>
>anything more than this seems less readable.  This has the added
>advantage of preventing readers and maintainers from getting confused 
>because of the differences between code at module level (where what feels 
>like a local is actually module-global), and in a function like main() 
>where locals are very clearly local.  I've seen __main__ blocks which
>are far too ugly to read, mainly because they are at module level instead
>of function-local.

No real argument, but I think that if you're doing that kind of
processing in main(), it should be called _main() to indicate that it's
private.  For example, you probably should not be calling open in a
main() function unless it's explicitly designed to only handle disk
files.  (Which is the case in this example; I'm giving more generic
advice.)
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"In many ways, it's a dull language, borrowing solid old concepts from
many other languages & styles:  boring syntax, unsurprising semantics,
few automatic coercions, etc etc.  But that's one of the things I like
about it."  --Tim Peters on Python, 16 Sep 93




More information about the Python-list mailing list