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

alex23 wuwei23 at gmail.com
Tue Oct 4 00:35:16 EDT 2011


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 :)


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /