M
Manuel Graune
Hello everyone,
the standard structure of a python-program which is taught in all of
the books I on python I read by now is simply something like:
#!/usr/bin/python
print "Hello, world!"
^D
While reading about structuring a larger code-base, unit-testing, etc
I stumbled on the idiom
#!/usr/bin/python
def main():
print "Hello, world"
if __name__ == "__main__":
main()
^D
While experimenting with this I found that the second version in most
cases is *a lot* faster than the simple approach. (I tried this both
on Linux and Windows) I found this even in cases where the code con-
sists simply of something like
j=0
for i in xrange(1000000):
j+=i
print j
How come the main()-idiom is not "the standard way" of writing a
python-program (like e.g. in C)?
And in addition: Can someone please explain why the first version
is so much slower?
Regards,
Manuel
the standard structure of a python-program which is taught in all of
the books I on python I read by now is simply something like:
#!/usr/bin/python
print "Hello, world!"
^D
While reading about structuring a larger code-base, unit-testing, etc
I stumbled on the idiom
#!/usr/bin/python
def main():
print "Hello, world"
if __name__ == "__main__":
main()
^D
While experimenting with this I found that the second version in most
cases is *a lot* faster than the simple approach. (I tried this both
on Linux and Windows) I found this even in cases where the code con-
sists simply of something like
j=0
for i in xrange(1000000):
j+=i
print j
How come the main()-idiom is not "the standard way" of writing a
python-program (like e.g. in C)?
And in addition: Can someone please explain why the first version
is so much slower?
Regards,
Manuel