Re: why a main() function?

2006-09-20 Thread Duncan Booth
[EMAIL PROTECTED] wrote: > [EMAIL PROTECTED] wrote: >> I think I read a suggestion somewhere to wrap the code where a Python >> script starts in a main() function, so one has > > > >> What are the advantages of doing this? > > Others have stated all the good ones, so I'll state a slightly dumbe

Re: why a main() function?

2006-09-19 Thread anton . list
[EMAIL PROTECTED] wrote: > I think I read a suggestion somewhere to wrap the code where a Python > script starts in a main() function, so one has > What are the advantages of doing this? Others have stated all the good ones, so I'll state a slightly dumber one for us part time amateur hackers :

Re: why a main() function?

2006-09-19 Thread Steven Bethard
Steve Holden wrote: > [EMAIL PROTECTED] wrote: >> I think I read a suggestion somewhere to wrap the code where a Python >> script starts in a main() function, so one has >> >> def main(): >> print "hi" >> >> main() >> >> instead of >> >> print "hi" >> >> What are the advantages of doing this? >

Re: why a main() function?

2006-09-19 Thread Fredrik Lundh
Diez B. Roggisch wrote: > Interesting. How is the index computed? I would have assumed that locals() > is somehow used, which is a dicht. > > I can imagine enumerating left-hand-side names and trying to replace their > occurence with the index, falling back to the name if that is not > possible/t

Re: why a main() function?

2006-09-19 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Paul Rubin wrote: > "Diez B. Roggisch" <[EMAIL PROTECTED]> writes: >> > Python stores local variables in an indexed array, but globals in a >> > dictionary. Looking things up by index is faster than looking them up by >> > name. >> >> Interesting. How is the index computed

Re: why a main() function?

2006-09-19 Thread Paul Rubin
"Diez B. Roggisch" <[EMAIL PROTECTED]> writes: > > Python stores local variables in an indexed array, but globals in a > > dictionary. Looking things up by index is faster than looking them up by > > name. > > Interesting. How is the index computed? I would have assumed that locals() > is somehow

Re: why a main() function?

2006-09-19 Thread Diez B. Roggisch
Fredrik Lundh wrote: > "Diez B. Roggisch" wrote: > >>> There is another secondary advantage: the code inside a function runs >>> faster (something related is true for C programs too). Usually this >>> isn't important, but for certain programs they can go 20%+ faster. >> >> I totally fail to see w

Re: why a main() function?

2006-09-19 Thread Peter Otten
Diez B. Roggisch wrote: > [EMAIL PROTECTED] wrote: > >> Others have already told you the most important things. >> >> There is another secondary advantage: the code inside a function runs >> faster (something related is true for C programs too). Usually this >> isn't important, but for certain p

Re: why a main() function?

2006-09-19 Thread Fredrik Lundh
"Diez B. Roggisch" wrote: >> There is another secondary advantage: the code inside a function runs >> faster (something related is true for C programs too). Usually this >> isn't important, but for certain programs they can go 20%+ faster. > > I totally fail to see why that should be the case - fo

Re: why a main() function?

2006-09-19 Thread Simon Brunning
On 9/19/06, Diez B. Roggisch <[EMAIL PROTECTED]> wrote: > I totally fail to see why that should be the case - for python as well as > for C. If you put your code into a main() function, all the names that it binds are in the function's local scope, whereas if the code is in the module's top level

Re: why a main() function?

2006-09-19 Thread Diez B. Roggisch
[EMAIL PROTECTED] wrote: > Others have already told you the most important things. > > There is another secondary advantage: the code inside a function runs > faster (something related is true for C programs too). Usually this > isn't important, but for certain programs they can go 20%+ faster.

Re: why a main() function?

2006-09-19 Thread billie
Another advantage is that you can catch all the unhandled exceptions of the entire program (it they occurs) by doing something like this: def another_call(): raise SomeUnexpectedException # it will be catched in '__main__' def call(): another_call() def run(): call() in __name__ =

[OT] Re: why a main() function?

2006-09-18 Thread Dan Sommers
On 18 Sep 2006 14:38:12 -0700, [EMAIL PROTECTED] wrote: > ... There is another secondary advantage: the code inside a function > runs faster (something related is true for C programs too). Usually > this isn't important, but for certain

Re: why a main() function?

2006-09-18 Thread Ben Finney
Steve Holden <[EMAIL PROTECTED]> writes: > [EMAIL PROTECTED] wrote: > > I think I read a suggestion somewhere to wrap the code where a > > Python script starts in a main() function > > [...] > > What are the advantages of doing this? > > > Guido van Rossum himself can tell you: >http://www.ar

Re: why a main() function?

2006-09-18 Thread bearophileHUGS
Others have already told you the most important things. There is another secondary advantage: the code inside a function runs faster (something related is true for C programs too). Usually this isn't important, but for certain programs they can go 20%+ faster. Bye, bearophile -- http://mail.pyt

Re: why a main() function?

2006-09-18 Thread Steve Holden
[EMAIL PROTECTED] wrote: > I think I read a suggestion somewhere to wrap the code where a Python > script starts in a main() function, so one has > > def main(): > print "hi" > > main() > > instead of > > print "hi" > > What are the advantages of doing this? > Guido van Rossum himself can

Re: why a main() function?

2006-09-18 Thread Benjamin Niemann
[EMAIL PROTECTED] wrote: > I think I read a suggestion somewhere to wrap the code where a Python > script starts in a main() function, so one has > > def main(): > print "hi" > > main() > > instead of > > print "hi" > > What are the advantages of doing this? Refine this to: def main():

Re: why a main() function?

2006-09-18 Thread Calvin Spealman
On 18 Sep 2006 12:40:00 -0700, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > I think I read a suggestion somewhere to wrap the code where a Python > script starts in a main() function, so one has > > def main(): > print "hi" > > main() > > instead of > > print "hi" > > What are the advantages

Re: why a main() function?

2006-09-18 Thread [EMAIL PROTECTED]
[EMAIL PROTECTED] wrote: > I think I read a suggestion somewhere to wrap the code where a Python > script starts in a main() function, so one has > > def main(): > print "hi" > > main() > > instead of > > print "hi" > > What are the advantages of doing this? I'm sure there are other reasons,

why a main() function?

2006-09-18 Thread beliavsky
I think I read a suggestion somewhere to wrap the code where a Python script starts in a main() function, so one has def main(): print "hi" main() instead of print "hi" What are the advantages of doing this? -- http://mail.python.org/mailman/listinfo/python-list