Is it possible to create C-style "main" function in Python? (for teaching purposes)

Westley Martínez anikom15 at gmail.com
Tue Oct 4 12:44:40 EDT 2011


On Mon, Oct 03, 2011 at 09:35:16PM -0700, alex23 wrote:
> Sorry for hijacking Alec's response but I didn't see the OP.
> 
> > Aivar Annamaa <aivar.anna... at gmail.com> wrote:
> > > I'm looking for a trick or hidden feature to make Python 3 automatically
> > > call a "main" function but without programmers writing `if __name__ ==
> > > "__main__": ...`
> 
> One direct way is to call it from the command line:
> 
>    python -c "import mymodule; mymodule.main()"
> 
> After your students have had to use that verbose form for a while,
> they'll be more than happy to add the boilerplate themselves to the
> end of their modules :)

Boiler plate is silly.  Let the students figure out stuff themselves.
The students need to know why global variables in functions is
unwieldly, not just not use them because it's cool.  When I taught
myself Python I quickly realized global variables were unwieldly and
usually impractical after using them.



More information about the Python-list mailing list