Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

newbie question

1 view
Skip to first unread message

Nan

unread,
Nov 26, 2008, 2:11:53 PM11/26/08
to
Hello,
I just started to use Python. I wrote the following code and
expected 'main' would be called.

def main():
print "hello"

main

But I was wrong. I have to use 'main()' to invoke main. The python
interpreter does not give any warnings for the above code. Is there
any way/tool to easily detect this kind of errors ?

Thanks !

Albert Hopkins

unread,
Nov 26, 2008, 2:21:44 PM11/26/08
to pytho...@python.org

Syntactically your code is correct, so the interpreter just goes about
its business. You can usually use tools such as pylint or pychecker to
find find such issues. For example, pylint reports (among other things):

W: 4: Statement seems to have no effect


Chris Rebert

unread,
Nov 26, 2008, 2:24:50 PM11/26/08
to Nan, pytho...@python.org

Python has first-class functions, so you can pass a function to
another function (so the line `main` has a meaning, just not a useful
one). Also, Python doesn't do compile-time typechecking, so it has no
way of knowing that `main` is a function and not a plain variable
until runtime. Thus, the parentheses are required for a function call
even when there are no arguments.

Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com

>
> Thanks !
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Asun Friere

unread,
Nov 27, 2008, 12:17:23 AM11/27/08
to
On Nov 27, 6:11 am, Nan <nan.l...@gmail.com> wrote:
> Hello,
>    I just started to use Python. I wrote the following code and
> expected 'main' would be called.
>
> def main():
>   print "hello"
>
> main

Not an answer to your question, but I dislike functions named 'main'
because the situation they occur in would better be dealt with by
exploiting the name of the built-in module '__main__'. But maybe
that's just me.

However, consider your code rewritten thusly:

def greet () :
print "hello"

if __name__ == '__main__' :
greet()

(A more literal translation of your program would be:
if __name__ == '__main__' : print 'hello')

This little trick ensures that greet() will execute if the module is
itself executed as a script, but that it won't if you import it from
elsewhere (ie. another script or the intepreter).

IMHO, it's good practice, wherever you may be tempted to write 'def
main()', intending this to be the glue code for your various functions
etc, instead to test whether the code is running as __main__ as above.

Bruno Desthuilliers

unread,
Nov 27, 2008, 5:05:30 AM11/27/08
to
Asun Friere a �crit :

The problem is that you often have more to do in the __main__ section of
a script than just calling one simple function, and you don't
necessarily want to pollute the module's namespace with all this code.
What I usually do is to have a main function called from the __main__
section, ie:


# myscript.py
# imports here
# classes and functions def here

def main(argv):
# parse args
# do whathever
return exit_status

if __name__ == "__main__":
import sys
sys.exit(main(sys.argv))

Asun Friere

unread,
Nov 27, 2008, 8:58:55 PM11/27/08
to
On Nov 27, 9:05 pm, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalid> wrote:

> The problem is that you often have more to do in the __main__ section of
> a script than just calling one simple function, and you don't
> necessarily want to pollute the module's namespace with all this code.

As I said, it's probably just me ;) I'm not sure I understand,
however, why calling multiple functions under 'if __name__ ...' will
pollute the namespace if these functions are already defined there?

Eg.

def foo (arg) :
#stuff
return exit_status
def bar (arg) :
#stuff
return exit_status


def main (argv) :
#parse args

#call and do stuff with foo and bar
return exit_status

if __name__ == '__main__' :
import sys
sys.exit(main(sys.argv))

Doesn't this just pollute the namespace with main()?

Surely you should be structuring the code above the test to minimise
namespace pollution.


Steven D'Aprano

unread,
Nov 27, 2008, 9:25:52 PM11/27/08
to
On Thu, 27 Nov 2008 17:58:55 -0800, Asun Friere wrote:


> if __name__ == '__main__' :
> import sys
> sys.exit(main(sys.argv))
>
> Doesn't this just pollute the namespace with main()?

Agreed. I don't see anything wrong with that. You have one function more
than you otherwise would have had, and you get the benefit that after
importing the module, you can say module.main(myargs) to run the code
just as if you had run it from the command line.

In other words... in my opinion, writing a non-trivial main() function is
the right thing to do.


--
Steven

David C. Ullrich

unread,
Dec 2, 2008, 12:13:21 PM12/2/08
to
In article
<f2fd606a-d92b-434f...@h5g2000yqh.googlegroups.com>,
Nan <nan....@gmail.com> wrote:

It's valid Python - it's only an error because it doesn't
do what you want.

The reason you're required to include the parentheses with
a function call is that in Python there are _other_ things
you might want to do with a function other than call it.
For example you can pass a function as a parameter to another
function. Silly example:

def twice(f):
f()
f()

def main():
print 'hello'

twice(main)

Before trying it, figure out what would happen if you said
twice(main()) .

A slightly more interesting example: twice(f) simply calls
f twice. double(f) returns a new function; when you call that
new function it calls f twice:

def double(f):
def res():
f()
f()
return res

def main():
print 'hello'

The point being that this example shows how sometimes you
want those parentheses and sometimes you don't. Either one
of the following is a way to call main twice:

mainmain = double(main)
mainmain()

or

double(main)()

When I said mainmain = double(main) I left off the
final parentheses because I didn't want to call mainmain
just then, I just wanted to set mainmain to the right thing.

> Thanks !

--
David C. Ullrich

0 new messages