Re: Is there any advantage to using a main() in python scripts?

2013-12-11 Thread Terry Reedy
On 12/11/2013 5:26 AM, Ben Finney wrote: Better design is to make the argument list a parameter to the ‘main’ function; this allows constructing an argument list specially for calling that function, without ‘main’ needing to know the difference. You'll also want to catch SystemExit and return t

Re: Is there any advantage to using a main() in python scripts?

2013-12-11 Thread Chris Angelico
On Thu, Dec 12, 2013 at 2:42 AM, bob gailer wrote: > It also ensures that the defining all the classes and functions happens > before referencing them (less "bookkeeping" for me). > > These two allow me to write the main program first, and follow it with all > the global stuff. I prefer define-be

Re: Is there any advantage to using a main() in python scripts?

2013-12-11 Thread bob gailer
On 12/11/2013 4:55 AM, JL wrote: What is the advantage to using a main()? In addition to what's been said I add: It separates all the global activities: defining of functions and classes, importing modules, etc. from the "doing" the actual task of the program. It also ensures that the defin

Re: Is there any advantage to using a main() in python scripts?

2013-12-11 Thread rusi
On Wednesday, December 11, 2013 7:47:34 PM UTC+5:30, Roy Smith wrote: > JL wrote: > > Python scripts can run without a main(). What is the advantage to using a > > main()? Is it necessary to use a main() when the script uses command line > > arguments? (See script below) > > #!/usr/bin/python

Re: Is there any advantage to using a main() in python scripts?

2013-12-11 Thread Roy Smith
In article , mar...@letterboxes.org wrote: > I would agree with the previous post but also add that I've stopped > calling the main function "main()" and usually give it a more > descriptive name, such as "bake_cookies()" or whatever. I think that > that makes it clearer what it's doing when use

Re: Is there any advantage to using a main() in python scripts?

2013-12-11 Thread Roy Smith
In article <32615c9a-b983-4399-bb55-6df6c230f...@googlegroups.com>, JL wrote: > Python scripts can run without a main(). What is the advantage to using a > main()? Is it necessary to use a main() when the script uses command line > arguments? (See script below) > > #!/usr/bin/python > > impo

Re: Is there any advantage to using a main() in python scripts?

2013-12-11 Thread marduk
I would agree with the previous post but also add that I've stopped calling the main function "main()" and usually give it a more descriptive name, such as "bake_cookies()" or whatever. I think that that makes it clearer what it's doing when used as a library and the 'if __name__ == '__main__'" a

Re: Is there any advantage to using a main() in python scripts?

2013-12-11 Thread Chris Angelico
On Wed, Dec 11, 2013 at 9:26 PM, Ben Finney wrote: > except SystemExit, exc: For new code, you'd of course want to write that as: except SystemExit as exc: which is compatible with Python 2.6, 2.7, and 3.x, while the other syntax is 2.x only. ChrisA -- https://mail.python.org/mailman/

Re: Is there any advantage to using a main() in python scripts?

2013-12-11 Thread Ben Finney
JL writes: > Python scripts can run without a main(). What is the advantage to > using a main()? Modular code – that is, implementing the program functionality in small, well-defined units with narrow, strictly-defined interfaces – is good design. Practical benefits include being able to easily

Is there any advantage to using a main() in python scripts?

2013-12-11 Thread JL
Python scripts can run without a main(). What is the advantage to using a main()? Is it necessary to use a main() when the script uses command line arguments? (See script below) #!/usr/bin/python import sys def main(): # print command line arguments for arg in sys.argv[1:]: pri

Re: main in Python

2009-07-10 Thread Benjamin Kaplan
On Fri, Jul 10, 2009 at 5:52 PM, Tim wrote: > > Hi, > I learned that a Python script is written in this way: > def main(): >    ... > if __name__ == "__main__": >    main() > > Today, when I read a script, I found it has a different way: > > def main(): >    ... > > main() > > It can run as well. C

Re: main in Python

2009-07-10 Thread Tim Harig
On 2009-07-10, Tim wrote: [RE-ORDERED] > It can run as well. Can someone explain why and the rules that Python > scripts get run? Everything gets run by default. The def syntax defines functions but does not run them -- they are only run when they are called > Today, when I read a script, I fou

main in Python

2009-07-10 Thread Tim
Hi, I learned that a Python script is written in this way: def main(): ... if __name__ == "__main__": main() Today, when I read a script, I found it has a different way: def main(): ... main() It can run as well. Can someone explain why and the rules that Python scripts get run?