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

Ian Kelly ian.g.kelly at gmail.com
Mon Oct 3 15:37:56 EDT 2011


On Mon, Oct 3, 2011 at 1:14 PM, Dave Angel <davea at ieee.org> wrote:
> Is it explaining the if statement that's the problem?  If so, you could have
> them do an unconditional main(sys.argv) at the bottom of their file, and not
> bother putting an if statement in front of it.  Then when you get to
> user-written modules, you could introduce the if __name__ and explain its
> need.

It's not a good idea to teach bad habits they'll just have to unlearn
later on.  Especially since they might not be paying attention when
you talk about if __name__ and how to do it right.

I would suggest giving them the whole if __name__ block as boilerplate
and telling them they're not allowed to alter it.  Then in a later
class, explain its purpose.



More information about the Python-list mailing list