Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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

 
 
Alec Taylor
Guest
Posts: n/a
 
      10-03-2011
Make something with http://metapython.org/

?

On Tue, Oct 4, 2011 at 3:21 AM, Aivar Annamaa <(E-Mail Removed)> 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
>
>

 
Reply With Quote
 
 
 
 
alex23
Guest
Posts: n/a
 
      10-04-2011
Sorry for hijacking Alec's response but I didn't see the OP.

> Aivar Annamaa <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Westley Martínez
Guest
Posts: n/a
 
      10-04-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 <(E-Mail Removed)> 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.
 
Reply With Quote
 
Alan Meyer
Guest
Posts: n/a
 
      10-05-2011
On 10/3/2011 12:26 PM, Alec Taylor wrote:
....
> On Tue, Oct 4, 2011 at 3:21 AM, Aivar Annamaa<(E-Mail Removed)> 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
 
Reply With Quote
 
Alan Meyer
Guest
Posts: n/a
 
      10-05-2011
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
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-05-2011
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
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      10-05-2011
On Wed, Oct 5, 2011 at 12:22 PM, Alan Meyer <(E-Mail Removed)> 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
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      10-05-2011
In article <(E-Mail Removed)>,
Chris Angelico <(E-Mail Removed)> 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.
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      10-05-2011
On Wed, Oct 5, 2011 at 11:57 PM, Roy Smith <(E-Mail Removed)> wrote:
> In article <(E-Mail Removed)>,
> *Chris Angelico <(E-Mail Removed)> 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
 
Reply With Quote
 
alex23
Guest
Posts: n/a
 
      10-06-2011
On Oct 5, 11:10*pm, Chris Angelico <(E-Mail Removed)> 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Is it possible to create C-style "main" function in Python? (forteaching purposes) Dave Angel Python 15 10-09-2011 08:31 PM
It is not possible to create a recursive function over a pyGTK treeStore sebastien.abeille@gmail.com Python 2 06-29-2007 12:53 PM
is it possible to possible to create an iterator from a callback interace? aninnymouse@gmail.com C Programming 4 02-21-2006 02:10 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
can i create a xalan java extension function where the function name has dashes (-) in it ? _clb_ Chris Bedford Java 0 08-17-2003 12:57 AM



Advertisments