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

2011-10-09 Thread 88888 dihedral
I do not think C is not good for functional programming, but C is hard to debug 
if one has to write programs to reload functional pointers  and data structures 
that will grow in the run time for the possible cases. Thus, I love Python!   
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-08 Thread Tim Roberts
Dennis Lee Bieber wlfr...@ix.netcom.com wrote:

   While I wouldn't want to write an FFT in COBOL, one can't deny that
laying out fixed width reports and moving blocks of decimal data between
record layouts is quite easy in COBOL.

Absolutely.  I've always thought the Data Section in COBOL was conceptually
ahead of its time.  It makes you THINK about your data structures more
than, say struct in C.
-- 
Tim Roberts, t...@probo.com
Providenza  Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-06 Thread alex23
Dennis Lee Bieber wlfr...@ix.netcom.com wrote:
         While I wouldn't want to write an FFT in COBOL, one can't deny that
 laying out fixed width reports and moving blocks of decimal data between
 record layouts is quite easy in COBOL.

Well, sure, but there's still plenty of pain in the verbosity :)
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-05 Thread Gregory Ewing

alex23 wrote:


But on the gripping hand, it is a clear triumph of Explicit is better
than implicit. ;)


I think we may have found the long-lost 20th principle
of the Zen: If it results in eye-bleedingly horrible
code, it might be a bad idea.

--
Greg
--
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-05 Thread Chris Angelico
On Wed, Oct 5, 2011 at 12:22 PM, Alan Meyer amey...@yahoo.com 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 should get
 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
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-05 Thread Ben Finney
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info writes:

 import the module called 'math' into the current namespace and bind
 that module to the name 'math' in the current namespace

 bind the name 'x' in the current namespace to the return result of
 calling the attribute named 'sin' in the object currently bound to the
 name 'math' in the current namespace using the float literal 1.2345 as
 the argument

This mocking is hurtful to people who identify too strongly with COBOL.
I wonder whether that means it's intentionally hurtful.

-- 
 \   “The long-term solution to mountains of waste is not more |
  `\  landfill sites but fewer shopping centres.” —Clive Hamilton, |
_o__)_Affluenza_, 2005 |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-05 Thread Roy Smith
In article mailman.1737.1317798109.27778.python-l...@python.org,
 Chris Angelico ros...@gmail.com 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-05 Thread Chris Angelico
On Wed, Oct 5, 2011 at 11:57 PM, Roy Smith r...@panix.com wrote:
 In article mailman.1737.1317798109.27778.python-l...@python.org,
  Chris Angelico ros...@gmail.com 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
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-05 Thread Westley Martínez
On Tue, Oct 04, 2011 at 08:20:34PM -0700, alex23 wrote:
 Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote:
  Imported modules are variables like any other, and as they usually exist
  in the global scope, so they will all need to be explicitly referenced as
  global. This will get tiresome very quickly, and is a cure far worse than
  the disease, and alone is enough to disqualify this suggestion from
  serious consideration.
 
 But on the gripping hand, it is a clear triumph of Explicit is better
 than implicit. ;)


Simple is better than complex.
Readability counts.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-05 Thread alex23
Ben Finney ben+pyt...@benfinney.id.au wrote:
 This mocking is hurtful to people who identify too strongly with COBOL.
 I wonder whether that means it's intentionally hurtful.

Far, _far_ less hurtful than COBOL itself...

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-05 Thread alex23
On Oct 5, 11:10 pm, Chris Angelico ros...@gmail.com 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/
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-05 Thread Cameron Simpson
On 03Oct2011 13:10, rantingrick rantingr...@gmail.com wrote:
| Also for scoping.
| 
| py count = 0
| py def foo():
| ... global.count += 1
| py print count
| 1
| 
| Why? Well because many times i find myself wondering if this or that
| variable is local or global -- and when i say global i am speaking
| of module scope! The globalDOT cures the ill.

I must admit I rarely have this concern. My own module globals are
almost entirely CONSTANT type names. (Excluding function and class
names.) 

What's the common ambifuity case for you?
-- 
Cameron Simpson c...@zip.com.au DoD#743
http://www.cskk.ezoshosting.com/cs/

Generally, these things are dreadful, but I saw a clip the other night on tv
of someone who had built a scorpion costume for their spaniel, complete with
legs and a stinger.  It was quite impressive.  Made me want to run out and
buy a dog and a some foam rubber.   - David Farley
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-05 Thread Westley Martínez
On Thu, Oct 06, 2011 at 02:44:33PM +1100, Cameron Simpson wrote:
 On 03Oct2011 13:10, rantingrick rantingr...@gmail.com wrote:
 | Also for scoping.
 | 
 | py count = 0
 | py def foo():
 | ... global.count += 1
 | py print count
 | 1
 | 
 | Why? Well because many times i find myself wondering if this or that
 | variable is local or global -- and when i say global i am speaking
 | of module scope! The globalDOT cures the ill.
 
 I must admit I rarely have this concern. My own module globals are
 almost entirely CONSTANT type names. (Excluding function and class
 names.) 
 
 What's the common ambifuity case for you?

I never have this concern either.  Python's functions and classes are
powerful enough to avoid globals entirely.  In C I have a few sometimes
and in Fortran and the like they're everywhere.  Global variables are
POWERFUL and USEFUL but there's a certain paradigm that goes with them,
and Python works better with an object-oriented w/ functional elements
approach.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-05 Thread Chris Angelico
On Thu, Oct 6, 2011 at 2:36 PM, alex23 wuwe...@gmail.com wrote:
 On Oct 5, 11:10 pm, Chris Angelico ros...@gmail.com 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
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-05 Thread Hansmeet Singh
REMEMBER STEVE

On Wed, Oct 5, 2011 at 9:58 PM, Chris Angelico ros...@gmail.com wrote:

 On Thu, Oct 6, 2011 at 2:36 PM, alex23 wuwe...@gmail.com wrote:
  On Oct 5, 11:10 pm, Chris Angelico ros...@gmail.com 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
 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-04 Thread Aivar Annamaa
Thanks for all the comments!
It seems that the best way is still just to teach students self discipline.
And hope that they (for now) believe some things (eg. dangers of global
variables) without seeing.

Aivar
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-04 Thread Westley Martínez
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...@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.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-04 Thread Ben Finney
Westley Martínez aniko...@gmail.com writes:

 Boiler plate is silly.  Let the students figure out stuff themselves.

That argues directly against the Primacy effect mentioned earlier
URL:https://secure.wikimedia.org/wikipedia/en/wiki/Principles_of_learning#Primacy
URL:https://secure.wikimedia.org/wikipedia/en/wiki/Serial_position_effect,
which is demonstrated by much scientific evidence. What scientific
evidence do you have to argue against it?

 The students need to know why global variables in functions is
 unwieldly, not just not use them because it's cool.

If you say so. But why not show them the traps after *first* teaching
them the recommended way to do it?

 When I taught myself Python I quickly realized global variables were
 unwieldly and usually impractical after using them.

That's no argument at all against how to teach Python in a
teacher-student relationship.

-- 
 \“If you have the facts on your side, pound the facts. If you |
  `\ have the law on your side, pound the law. If you have neither |
_o__)   on your side, pound the table.” —anonymous |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-04 Thread Alan Meyer

On 10/3/2011 12:26 PM, Alec Taylor wrote:
...

On Tue, Oct 4, 2011 at 3:21 AM, Aivar Annamaaaivar.anna...@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__: ...`

...

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
--
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-04 Thread Alan Meyer

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
--
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-04 Thread Steven D'Aprano
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
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-04 Thread Steven D'Aprano
On Mon, 03 Oct 2011 21:27:12 -0700, alex23 wrote:

 rantingrick rantingr...@gmail.com wrote:
 Why? Well because many times i find myself wondering if this or that
 variable is local or global -- and when i say global i am speaking of
 module scope! The globalDOT cures the ill.
 
 Given your stated propensity for huge code blocks not chunked into
 functions, I'm not surprised you lose track of what is global, what is
 nonlocal etc. This is another clear advantage of small functions: you
 can view it all at once. For the rest of us, a variable is global if its
 referenced but not defined in a specific scope. There's no need for such
 verbose hand-holding.
 
 I'd say the wart is in your design practice rather than the language
 itself.

Furthermore, rick's suggestion that all globals should be referenced 
using an explicit global namespace would become extraordinarily horrible 
in practice. Let's take a simple example from the shutil module:

# From Python 2.5 shutil.py
def copystat(src, dst):
Copy all stat info (mode bits, atime and mtime) from src to dst
st = os.stat(src)
mode = stat.S_IMODE(st.st_mode)
if hasattr(os, 'utime'):
os.utime(dst, (st.st_atime, st.st_mtime))
if hasattr(os, 'chmod'):
os.chmod(dst, mode)


Under Rick's proposal, that would be written:

def copystat(src, dst):
Copy all stat info (mode bits, atime and mtime) from src to dst
st = global.os.stat(src)
mode = global.stat.S_IMODE(st.st_mode)
if global.hasattr(os, 'utime'):
global.os.utime(dst, (st.st_atime, st.st_mtime))
if global.hasattr(os, 'chmod'):
global.os.chmod(dst, mode)


Imported modules are variables like any other, and as they usually exist 
in the global scope, so they will all need to be explicitly referenced as 
global. This will get tiresome very quickly, and is a cure far worse than 
the disease, and alone is enough to disqualify this suggestion from 
serious consideration.

Furthermore, globals in Python also includes the built-ins, so every 
reference to built-ins like len, sum, list, int, abs, etc. will also need 
an explicit reference, e.g. global.len, global.sum.

(The alternative would be a significantly different name-lookup strategy: 
instead of names being local vs global or builtin, they would be 
local or builtin vs global. This will break monkey-patching.)


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-04 Thread alex23
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote:
 Imported modules are variables like any other, and as they usually exist
 in the global scope, so they will all need to be explicitly referenced as
 global. This will get tiresome very quickly, and is a cure far worse than
 the disease, and alone is enough to disqualify this suggestion from
 serious consideration.

But on the gripping hand, it is a clear triumph of Explicit is better
than implicit. ;)

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-04 Thread Steven D'Aprano
On Tue, 04 Oct 2011 20:20:34 -0700, alex23 wrote:

 Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote:
 Imported modules are variables like any other, and as they usually
 exist in the global scope, so they will all need to be explicitly
 referenced as global. This will get tiresome very quickly, and is a
 cure far worse than the disease, and alone is enough to disqualify this
 suggestion from serious consideration.
 
 But on the gripping hand, it is a clear triumph of Explicit is better
 than implicit. ;)


I see your wink, but explicitness is not a binary state. You can have too 
much explicitness. Which would you rather?


import math
x = math.sin(1.2345)


or:


import the module called 'math' into the current namespace and bind that 
module to the name 'math' in the current namespace

bind the name 'x' in the current namespace to the return result of 
calling the attribute named 'sin' in the object currently bound to the 
name 'math' in the current namespace using the float literal 1.2345 as 
the argument


I'm thinking that the second version might get a bit annoying after a 
little while, no matter what the Zen says.

Besides, based on the Zen, should we also write this?

local.x = global.math.sin(1.2345)




-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-03 Thread Aivar Annamaa
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


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

2011-10-03 Thread Alec Taylor
Make something with http://metapython.org/

?

On Tue, Oct 4, 2011 at 3:21 AM, Aivar Annamaa aivar.anna...@gmail.com 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


-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-03 Thread Dave Angel

On 01/-10/-28163 02:59 PM, 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

It's not clear if you're asking for a new fork of the language, or just 
wanting to keep people from bad habits.


Like it or not, there are plenty of globals already there, one of them 
being __name__ .  All the built-ins are effectively global, and so
 is any function they define at top-level.  Likewise any top-level 
class, and any symbols imported with import or with from/import.  So I 
consider it impractical for the language to do something that 
self-discipline is required for.


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.


DaveA




--
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-03 Thread Ian Kelly
On Mon, Oct 3, 2011 at 1:14 PM, Dave Angel da...@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.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-03 Thread rantingrick
On Oct 3, 2:14 pm, Dave Angel da...@ieee.org wrote:

 Like it or not, there are plenty of globals already there, one of them
 being __name__ .  All the built-ins are effectively global, and so
   is any function they define at top-level.

I keep wondering if that was another PyWart? I believe (and hindsight
is 20-20) that all the built-in modules should have been protected by
a top-level namespace. Something succicent, something like py...

from py.archive import zipfile, tarfile
from py.gui import Tkinter
from py.markup import HTMLParser

...and voila, no more clashes with user defined modules!


 Likewise any top-level
 class, and any symbols imported with import or with from/import.  So I
 consider it impractical for the language to do something that
 self-discipline is required for.

Also for scoping.

py count = 0
py def foo():
... global.count += 1
py print count
1

Why? Well because many times i find myself wondering if this or that
variable is local or global -- and when i say global i am speaking
of module scope! The globalDOT cures the ill.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-03 Thread Gregory Ewing

Ian Kelly wrote:


I would suggest giving them the whole if __name__ block as boilerplate
and telling them they're not allowed to alter it.


I would suggest not showing them if __name__ at all;
instead encourage them to do this:

  def main():
...
...

  main()

You only need if __name__ == '__main__' if you want
a .py file that can be used as either a module or a
main program. Most of the time you *don't* need that.
I consider it advanced usage that beginners shouldn't
be confused with.

--
Greg
--
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-03 Thread Dave Angel

On 01/-10/-28163 02:59 PM, rantingrick wrote:

On Oct 3, 2:14 pm, Dave Angelda...@ieee.org  wrote:


Like it or not, there are plenty of globals already there, one of them
being __name__ .  All the built-ins are effectively global, and so
   is any function they define at top-level.

I keep wondering if that was another PyWart? I believe (and hindsight
is 20-20) that all the built-in modules


There's only one  __builtins__  module, which is implicitly loaded, and 
contains tons of things which are effectively global, such as open, 
float, filter, sorted, etc.



  should have been protected by
a top-level namespace. Something succicent, something like py...

from py.archive import zipfile, tarfile
from py.gui import Tkinter
from py.markup import HTMLParser

...and voila, no more clashes with user defined modules!

Gee, you just described a package.  So why not say that the stdlib 
should have been done as a package of modules ?  I don't know if I agree 
or not, just trying to keep things level.

  Likewise any top-level
class, and any symbols imported with import or with from/import.  So I
consider it impractical for the language to do something that
self-discipline is required for.

Also for scoping.

py  count =
py  def foo():
... global.count +=
py  print count
1

Why? Well because many times i find myself wondering if this or that
variable is local or global -- and when i say global i am speaking
of module scope! The globalDOT cures the ill.


DaveA
--
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-03 Thread Ian Kelly
On Mon, Oct 3, 2011 at 2:10 PM, rantingrick rantingr...@gmail.com wrote:
 Also for scoping.

 py count = 0
 py def foo():
 ...     global.count += 1
 py print count
 1

 Why? Well because many times i find myself wondering if this or that
 variable is local or global -- and when i say global i am speaking
 of module scope! The globalDOT cures the ill.

def add_from_input(num_lines):
total = global.sum(global.int(global.input()) for i in
global.range(num_lines))
global.print(The total is, total)

Yes, that's much better.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-03 Thread Chris Angelico
On Tue, Oct 4, 2011 at 9:41 AM, Ian Kelly ian.g.ke...@gmail.com wrote:
 def add_from_input(num_lines):
    total = global.sum(global.int(global.input()) for i in
 global.range(num_lines))
    global.print(The total is, total)

That's pretty unfair to the globals.

def add_from_input(param.num_lines):
   local.total = global.sum(global.int(global.input()) for tinyscope.i
in global.range(param.num_lines))
   global.print(The total is, local.total)

FTFY.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-03 Thread Roy Smith
In article mailman.1702.1317670708.27778.python-l...@python.org,
 Ian Kelly ian.g.ke...@gmail.com wrote:

 It's not a good idea to teach bad habits they'll just have to unlearn
 later on.

Absolutely correct.  People who teach teachers how to teach call this 
the Law of Primacy 
(http://en.wikipedia.org/wiki/Principles_of_learning#Primacy), and 
(despite a lot of the religious psychobabble that comes with the 
territory) it really is true.  Much better to just say, put this stuff 
at the end of your file and don't worry about it for now then to teach 
people the wrong way to do things.

At some point, you'll get up to talking about modules and/or the magic 
double-underscore namespace.  Then you'll have the opportunity to double 
back and explain what it means.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-03 Thread alex23
rantingrick rantingr...@gmail.com wrote:
 Why? Well because many times i find myself wondering if this or that
 variable is local or global -- and when i say global i am speaking
 of module scope! The globalDOT cures the ill.

Given your stated propensity for huge code blocks not chunked into
functions, I'm not surprised you lose track of what is global, what is
nonlocal etc. This is another clear advantage of small functions: you
can view it all at once. For the rest of us, a variable is global if
its referenced but not defined in a specific scope. There's no need
for such verbose hand-holding.

I'd say the wart is in your design practice rather than the language
itself.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2011-10-03 Thread alex23
Sorry for hijacking Alec's response but I didn't see the OP.

 Aivar Annamaa aivar.anna...@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 :)
-- 
http://mail.python.org/mailman/listinfo/python-list