Re: Is it possible to create C-style "main" function in Python? (forteaching purposes)

Discussion in 'Python' started by Alec Taylor, Oct 3, 2011.

  1. Alec Taylor

    Alec Taylor Guest

    Make something with http://metapython.org/

    ?

    On Tue, Oct 4, 2011 at 3:21 AM, Aivar Annamaa <> wrote:
    > Hi!
    >
    > 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__": ...`
    >
    > I found rejected PEP 299, but i thought that maybe there's something new
    > already.
    >
    > Here's why I want such a thing:
    > I'm teaching introductory programming course with Python. I've seen that
    > global variables attract beginners like honey attracts bees and this makes
    > teaching function parameters harder. When students learn functions, they
    > usually write their function definitions and function applications in the
    > same scope -- in top-level of the module (don't know the correct term for
    > it). This has the downside, that any variable introduced in top-level is
    > automatically visible in function definitions and I have hard time
    > convincing students not to use those variables in functions directly.
    >
    > I've been thinking that it might be better for teaching if all program code
    > would be in functions. This would make "Hello World" a bit more difficult,
    > but would help teaching the "real thing" ie. functions.
    >
    > best regards,
    > Aivar
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    >
    Alec Taylor, Oct 3, 2011
    #1
    1. Advertising

  2. Alec Taylor

    alex23 Guest

    Sorry for hijacking Alec's response but I didn't see the OP.

    > Aivar Annamaa <> 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 :)
    alex23, Oct 4, 2011
    #2
    1. Advertising

  3. 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 <> 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.
    Westley Martínez, Oct 4, 2011
    #3
  4. Alec Taylor

    Alan Meyer Guest

    On 10/3/2011 12:26 PM, Alec Taylor wrote:
    ....
    > On Tue, Oct 4, 2011 at 3:21 AM, Aivar Annamaa<> 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__": ...`

    ....
    >> Here's why I want such a thing:
    >> I'm teaching introductory programming course with Python. I've seen that
    >> global variables attract beginners like honey attracts bees and this makes
    >> teaching function parameters harder. ...


    Teaching good programming practice is hard. Many programmers, not just
    students, take the attitude, "My code works. Who cares what it looks
    like?" Getting them to understand that code has to be readable,
    understandable, and maintainable can be very hard.

    I wonder if some teaching exercises would help, for example:

    1. Find a program, perhaps submitted by a student in a previous class or
    perhaps something you write yourself, that's full of global variables.
    Assign the students to rewrite the program so that it has no globals at
    all, and to write up a comparison of the pros and cons of the global and
    no-global approaches.

    2. Find or write a program with lots of globals. Introduce some subtle
    bugs into the program that have to do with global references. Assign
    the students to a) find and fix the bugs and b) explain how the code
    could have been written to prevent bugs like this from creeping in.

    3. Find or write a program with a number of globals. For each global,
    ask the students to write an analysis comparing its usefulness and/or
    dangerousness. Are some of the globals worse than others? Why?

    4. Find or write a program with some globals. Make up a change request
    from a user that will run into problems because of the globals. Assign
    the students to implement the change request.

    There are probably lots of other similar exercises one could make up.

    The idea is not to force students to do the right thing, but to get them
    to understand the differences between the better ways and the worse ways
    to write code.

    Incidentally, all of these exercises involve maintaining or re-writing
    existing code written by other people. Students don't usually do much
    of that, but when they get a real job, they find that maintenance is
    most of what they actually do, especially as junior programmers. Having
    to work in the real world of maintaining other people's code gives a
    student a better appreciation of the value of clean, modular, readable,
    documented code.

    Alan
    Alan Meyer, Oct 5, 2011
    #4
  5. Alec Taylor

    Alan Meyer Guest

    On 10/4/2011 9:07 PM, Alan Meyer wrote:

    > ... and to write up a comparison of the pros and cons of the global and
    > no-global approaches. ...


    Of course you'll need to be fair in evaluating the students comparisons.
    Some bright students are likely to come up with good reasons for using
    globals in some situations, and they might even be right. Or if they're
    not completely right, they might nevertheless be partly right. They
    should get high marks for that.

    You could even make up an exercise where the students are assigned to
    write a program that uses a global that could NOT be better implemented
    without globals. Then ask one or two of the authors of the better
    programs to defend their programs in front of the class.

    It's always a mistake to read student papers with preconceived, set in
    concrete ideas about what's right and what's wrong. Many years ago when
    I was teaching (philosophy, not computer science), in every class I
    taught there was always at least one student who taught me something I
    didn't know, or taught me that something I thought I knew was wrong.

    Alan
    Alan Meyer, Oct 5, 2011
    #5
  6. Re: Is it possible to create C-style "main" function in Python?(for teaching purposes)

    On Tue, 04 Oct 2011 21:07:10 -0400, Alan Meyer wrote:

    > Incidentally, all of these exercises involve maintaining or re-writing
    > existing code written by other people. Students don't usually do much
    > of that, but when they get a real job, they find that maintenance is
    > most of what they actually do, especially as junior programmers. Having
    > to work in the real world of maintaining other people's code gives a
    > student a better appreciation of the value of clean, modular, readable,
    > documented code.


    Well said that man!!!



    --
    Steven
    Steven D'Aprano, Oct 5, 2011
    #6
  7. On Wed, Oct 5, 2011 at 12:22 PM, Alan Meyer <> wrote:
    > Of course you'll need to be fair in evaluating the students comparisons.
    >  Some bright students are likely to come up with good reasons for using
    > globals in some situations, and they might even be right.  Or if they're not
    > completely right, they might nevertheless be partly right.  They shouldget
    > high marks for that.
    >


    Definitely. There's always a right time to do the wrong thing, just as
    much as there's a wrong time to do the right thing. Even the
    much-maligned goto has its place.

    ChrisA
    Chris Angelico, Oct 5, 2011
    #7
  8. Alec Taylor

    Roy Smith Guest

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

    In article <>,
    Chris Angelico <> wrote:

    > Definitely. There's always a right time to do the wrong thing, just as
    > much as there's a wrong time to do the right thing. Even the
    > much-maligned goto has its place.


    Not in python, it doesn't :)

    But, yes, I agree that in languages that support it, it can be useful.
    When I was writing C++ for a living, I must have written a goto at least
    once every couple of years.
    Roy Smith, Oct 5, 2011
    #8
  9. On Wed, Oct 5, 2011 at 11:57 PM, Roy Smith <> wrote:
    > In article <>,
    >  Chris Angelico <> wrote:
    >
    >> Definitely. There's always a right time to do the wrong thing, just as
    >> much as there's a wrong time to do the right thing. Even the
    >> much-maligned goto has its place.

    >
    > Not in python, it doesn't :)


    The absence from the language doesn't prove that. All it means is
    that, on those rare occasions when a goto would have been correct, the
    programmer had to make do with something else :)

    How often do you see a loop structure that exists solely so someone
    can 'break' out of it? Or, worse, raising an exception? I haven't seen
    it in Python, but frequently in C or C++ code where the programmer had
    a fixation on avoiding gotos.

    ChrisA
    Chris Angelico, Oct 5, 2011
    #9
  10. Alec Taylor

    alex23 Guest

    On Oct 5, 11:10 pm, Chris Angelico <> wrote:
    > The absence from the language doesn't prove that. All it means is
    > that, on those rare occasions when a goto would have been correct, the
    > programmer had to make do with something else :)


    Like the goto module? :)

    http://entrian.com/goto/
    alex23, Oct 6, 2011
    #10
  11. On Thu, Oct 6, 2011 at 2:36 PM, alex23 <> wrote:
    > On Oct 5, 11:10 pm, Chris Angelico <> wrote:
    >> The absence from the language doesn't prove that. All it means is
    >> that, on those rare occasions when a goto would have been correct, the
    >> programmer had to make do with something else :)

    >
    > Like the goto module? :)
    >
    > http://entrian.com/goto/


    Yes. That module is extremely valuable and needs to be brought into
    the main trunk. Rick, can this go on your Python 4000 list?

    ChrisA
    Chris Angelico, Oct 6, 2011
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    354
    Steven D'Aprano
    May 15, 2005
  2. Ravi
    Replies:
    17
    Views:
    924
    Kenneth Brody
    Apr 1, 2006
  3. ravi
    Replies:
    28
    Views:
    978
    Richard Heathfield
    Sep 26, 2007
  4. Dave Angel
    Replies:
    15
    Views:
    263
    88888 dihedral
    Oct 9, 2011
  5. Luca Cerone
    Replies:
    29
    Views:
    1,365
    lucacerone
    Feb 21, 2012
Loading...

Share This Page