interactive vs compiled from file
One problem I have consistently had with beginners is in explaining the difference between how the interpreter produces output without an explicit 'print' statement (producing the str() of the last evaluated expression) and how python run from a file does not. This is perfectly natural and useful for an experienced user, but it is awkward and confusing for the beginner, who has enough of a cognitive load just trying to track all the pieces. This leads to two (well, four) questions: 1) How have you folks addressed this problem in your beginning classes? Do you find it causes difficulties? 2) Would it be difficult to provide an alternative interpreter that produced nothing unless you asked it to print? Would this be a good idea? thanks mt
On 8/3/07, Michael Tobis <mtobis@gmail.com> wrote:
One problem I have consistently had with beginners is in explaining the difference between how the interpreter produces output without an explicit 'print' statement (producing the str() of the last evaluated expression) and how python run from a file does not.
This is perfectly natural and useful for an experienced user, but it is awkward and confusing for the beginner, who has enough of a cognitive load just trying to track all the pieces.
This leads to two (well, four) questions:
1) How have you folks addressed this problem in your beginning classes? Do you find it causes difficulties?
2) Would it be difficult to provide an alternative interpreter that produced nothing unless you asked it to print? Would this be a good idea?
I'll reserve judgement as to whether or not this is a good idea, but it's possible to do - at least, according to a quick implementation I tried with Crunchy. *If* this is a desirable feature, and assuming my quick test is correct, I could include it as yet another type of interpreter available within Crunchy. :-) André P.S. it would need a name to distinguish it from the normal interpreter. (un-parrot?) thanks
mt _______________________________________________ Edu-sig mailing list Edu-sig@python.org http://mail.python.org/mailman/listinfo/edu-sig
On 8/3/07, Andre Roberge <andre.roberge@gmail.com> wrote:
On 8/3/07, Michael Tobis <mtobis@gmail.com> wrote:
One problem I have consistently had with beginners is in explaining the difference between how the interpreter produces output without an explicit 'print' statement (producing the str() of the last evaluated expression) and how python run from a file does not.
This is perfectly natural and useful for an experienced user, but it is awkward and confusing for the beginner, who has enough of a cognitive load just trying to track all the pieces.
This leads to two (well, four) questions:
1) How have you folks addressed this problem in your beginning classes? Do you find it causes difficulties?
2) Would it be difficult to provide an alternative interpreter that produced nothing unless you asked it to print?
If you want to give it a try, here's what you need to do. Find the code.py module in the standard library. Replace 'single' by 'exec' in runsource() i.e. def runsource(self, source, filename="<input>", symbol="single"): by def runsource(self, source, filename="<input>", symbol="exec"): To run it: import code code.interact() Unless I am missing something, this should do the trick. Ideally, one would change the prompt too so as to distinguish it from the regular interpreter. André Would this be a good
idea?
I'll reserve judgement as to whether or not this is a good idea, but it's possible to do - at least, according to a quick implementation I tried with Crunchy.
*If* this is a desirable feature, and assuming my quick test is correct, I could include it as yet another type of interpreter available within Crunchy. :-)
André
P.S. it would need a name to distinguish it from the normal interpreter. (un-parrot?)
thanks
mt _______________________________________________ Edu-sig mailing list Edu-sig@python.org http://mail.python.org/mailman/listinfo/edu-sig
On 8/3/07, Michael Tobis <mtobis@gmail.com> wrote:
One problem I have consistently had with beginners is in explaining the difference between how the interpreter produces output without an explicit 'print' statement (producing the str() of the last evaluated expression) and how python run from a file does not.
This is perfectly natural and useful for an experienced user, but it is awkward and confusing for the beginner, who has enough of a cognitive load just trying to track all the pieces.
This leads to two (well, four) questions:
1) How have you folks addressed this problem in your beginning classes? Do you find it causes difficulties?
2) Would it be difficult to provide an alternative interpreter that produced nothing unless you asked it to print? Would this be a good idea?
thanks mt
How about explain that Python in "shell mode" is actually like running a script such as this one: =========== Repl.py ============== # baby shell (cave painting) def repl(): while True: mycommand = raw_input(">>> ") # assignment or function def ? if "=" in mycommand or "def " in mycommand: # easily broken exec mycommand in locals(), globals() # quitting? elif mycommand == "exit()": break # treat as an expression else: # don't do anything stupid print eval(mycommand) repl() print "Thank you for using Python" In use: kirby@dell:~$ python ./repl.py
def f(x): return x * x f(10) 100 a = f(90) a 8100 1 + 2 3 exit() Thank you for using Python
Even if they don't get all the code, the point is that "shell mode" is an active evaluation loop with a raw_input like prompt and persistence in memory (scope). Simply feeding a mou.. er module to our snake makes it crunch through the code without invoking this "live action debugger" (this "shell"). Memory is still populated of course, but expressions evaluated without any namespace bindings may just vanish into thin air -- unless they have other side-effects -- because there's no 'print eval(myexpression)' loop in effect. Kirby
On 3-Aug-07, at 11:17 AM, Michael Tobis wrote:
2) Would it be difficult to provide an alternative interpreter that produced nothing unless you asked it to print? Would this be a good idea?
'abc' 'abc' import sys def f(value): pass ... sys.displayhook = f 'abc'
So if you add a do-nothing displayhook in your site.py, or as part of a standard local import, you won't have the echo behaviour. HTH --Dethe "It goes against the grain of modern education to teach children to program. What fun is there in making plans, acquiring discipline in organizing thoughts, devoting attention to detail and learning to be self-critical?" --Alan Perlis
Thanks, Dethe, that's very nice, and also thanks for your signature, which is thought provoking indeed. Does anyone agree with me about the didactic value of this change? mt On 8/3/07, Dethe Elza <delza@livingcode.org> wrote:
On 3-Aug-07, at 11:17 AM, Michael Tobis wrote:
2) Would it be difficult to provide an alternative interpreter that produced nothing unless you asked it to print? Would this be a good idea?
'abc' 'abc' import sys def f(value): pass ... sys.displayhook = f 'abc'
So if you add a do-nothing displayhook in your site.py, or as part of a standard local import, you won't have the echo behaviour.
HTH
--Dethe
"It goes against the grain of modern education to teach children to program. What fun is there in making plans, acquiring discipline in organizing thoughts, devoting attention to detail and learning to be self-critical?" --Alan Perlis
On 8/3/07, Michael Tobis <mtobis@gmail.com> wrote:
Thanks, Dethe, that's very nice, and also thanks for your signature, which is thought provoking indeed.
Does anyone agree with me about the didactic value of this change?
mt
If you put duct tape over the snake's mouth in shell mode, students might discover they miss the REPL, which helps motivate a sense of why so many successful languages (LISP, Scheme, APL, J... xBase) have such a mode. http://en.wikipedia.org/wiki/REPL (note: this article suggests not confusing REPL with "an interpreter" citing Python in particular). Asked what about Python most drew them in, many newbies cite shell mode (e.g. iPython, IDLE, PyCrust, DrPython or whatever). -- Kirby Another project in the Crunchy genre? http://projects.amor.org/misc/wiki/HTTPREPL
On 8/3/07, kirby urner <kirby.urner@gmail.com> wrote:
On 8/3/07, Michael Tobis <mtobis@gmail.com> wrote:
Thanks, Dethe, that's very nice, and also thanks for your signature, which is thought provoking indeed.
Yes, thanks Dethe. And your pointer about sys.displayhook may be just what was needed so that I could embed IPython as an alternative shell within Crunchy - without interfering with the other shells! I just need to experiment a bit more...
Does anyone agree with me about the didactic value of this change?
mt
Well, I do - I had to explain this very difference between the behaviour of the interpreter and that of an editor [inside Crunchy :-)] to my son last week; I managed to get him started on "How to think like a computer scientist". :-) :-) How would you call that mode to distinguish it from the standard interpreter, and warning your students that this is a non-standard implementation? If you put duct tape over the snake's mouth in shell mode,
students might discover they miss the REPL, which helps motivate a sense of why so many successful languages (LISP, Scheme, APL, J... xBase) have such a mode.
http://en.wikipedia.org/wiki/REPL
(note: this article suggests not confusing REPL with "an interpreter" citing Python in particular).
Asked what about Python most drew them in, many newbies cite shell mode (e.g. iPython, IDLE, PyCrust, DrPython or whatever).
Kirby got a good point - however, I think that you'd still get the "hook" with newbies without the automatic echoing that Michael describes. -- Kirby
Another project in the Crunchy genre? http://projects.amor.org/misc/wiki/HTTPREPL
Crunchy 0.1 was very much inspired by this same project from Robert Brewer - the original code built on that example. It wasn't until version 0.3, I believe, that Johannes Woolard removed the CherryPy dependency ( http://pytute.blogspot.com/2006/05/progress-so-far.html) allowing Crunchy to evolve significantly since. André _______________________________________________
Edu-sig mailing list Edu-sig@python.org http://mail.python.org/mailman/listinfo/edu-sig
Thanks, Dethe, that's very nice, and also thanks for your signature, which is thought provoking indeed.
I'm glad you found the snippet helpful (and also the sig).
Yes, thanks Dethe. And your pointer about sys.displayhook may be just what was needed so that I could embed IPython as an alternative shell within Crunchy - without interfering with the other shells! I just need to experiment a bit more...
That's cool, glad to play a part in the evolution of Crunchy.
Does anyone agree with me about the didactic value of this change?
Apart from the challenge of being able to turn off echoing, I'm not convinced it's a good idea. It seems to me that it should be relatively easy to explain. Also, with the naive replacement of the displayhook I demonstrated, you lose the underscore value (normally in the interpreter the _ variable holds the last result that was echoed). This is useful when exploring, because sometimes you will test several things, and then when it works, you have it saved in _ without having to retrace your steps. I'm more inclined to go further and use IPython (which has vastly *more* changes from the non- interactive Python runtime) for all the nice tools it gives explorers. On the other hand, my kids are just now starting to explore Python, so I don't have much practical experience to share. Yet. Unrelated digression: My six-year-old is trying to program Python in Scratch, which is interesting, because he doesn't really know anything about Python beyond "you type in words and it does stuff." So he's essentially inventing parsing on his own, in a language that makes text parsing amazingly difficult. Scratch makes getting the basics very easy, but I think by the time I get my PyGame-based scratch-like environment ready for my kids, they will find Python's relative lack of arbitrary limits to be very refreshing. --Dethe Every day computers are making people easier to use. --David Tompkin
On 8/3/07, Andre Roberge <andre.roberge@gmail.com> wrote:
Kirby got a good point - however, I think that you'd still get the "hook" with newbies without the automatic echoing that Michael describes.
Not sure we should call this "echoing" as what's happening is an expression is being evaluated. It just so happens that the eval of a quoted string returns itself whereas "some expression with %s" % "substitution" would actually evaluate to non-echo. kirby@dell:~$ python ./repl.py
'THIS IS A STRING' THIS IS A STRING [x*x for x in range(10)] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] "1-2-3 %s" % "testing" 1-2-3 testing
Would one turn off all of the above, or just the first eval-print? What would be the point of a broken shell, vs explaining that Python, like many languages, features a REPL (read-eval-print loop)? Kirby
On 8/3/07, kirby urner <kirby.urner@gmail.com> wrote:
On 8/3/07, Andre Roberge <andre.roberge@gmail.com> wrote:
Kirby got a good point - however, I think that you'd still get the "hook" with newbies without the automatic echoing that Michael describes.
Not sure we should call this "echoing" as what's happening is an expression is being evaluated.
It just so happens that the eval of a quoted string returns itself whereas "some expression with %s" % "substitution" would actually evaluate to non-echo.
kirby@dell:~$ python ./repl.py
'THIS IS A STRING' THIS IS A STRING [x*x for x in range(10)] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] "1-2-3 %s" % "testing" 1-2-3 testing
Would one turn off all of the above, or just the first eval-print?
What would be the point of a broken shell, vs explaining that Python, like many languages, features a REPL (read-eval-print loop)?
Kirby
I don't see it as "either or", but as "why not allow both?". I agree entirely with the strength of the REPL. However, when going from that to having code saved in a file and executed, a huge step occurs (which is what Michael was referring to I believe) in that to see something output, the user *must* put in an explicit print statement. What I envisage right now is to continue to have the default shell using in Crunchy (prompt = ">>>") and give the possibility to have a "parrot" shell (obligatory Monty Python reference; he's not dead. He may not be replying ... but he's just sleeping). Perhaps the prompt could be u_) (parrot lying on its side, two feet up, beak pointing up; sorry Kirby, the parrot's ribs are too small to see). I could see a teacher giving a few interactive examples with this shell, have the students play with it for a short while, and then move on to saving files and running them. ... Just something to ease the transition from the friendly REPL to writing standalone scripts. @Michael: is this what you had in mind? André
I don't see it as "either or", but as "why not allow both?". I agree entirely with the strength of the REPL. However, when going from that to having code saved in a file and executed, a huge step occurs (which is what Michael was referring to I believe) in that to see something output, the user *must* put in an explicit print statement.
OK, but there's a middle ground between REPL and running a .py file, which is importing library and 3rd party modules *in shell mode*. My preferred pedagogy (others prefer others) is to (a) start with REPL (e.g. "using Python as a calculator") then (b) move on to such as 'import math' but treating the module as a 'grab bag of tools' that we use *interactively* e.g. math.cos(someangle). At this point, we're ready for graphical forms of output (e.g. VPython cylinders and balls), possible robot control ala Logo. then finally: (c) standalone .py programs, perhaps taking command line inputs ala sys.args, perhaps prompting the user with an old fashioned menu. (d) if we get to it: (c) with the added benefit of a GUI. The transition from (a) to (b) is where we handle the steps of changing code in the module, doing a reload. Plus we have the fun of talking about sys.path, dir() and help(), talking about namespaces more generally, 'import foo' versus 'from foo import x as y'. The transition to (c) involves looking at python the command line executable, going python --h for the first time (looking a switches). The transition to (d) is where many a Visual Basic programmer thinks we should *start* e.g. with some simple standalone GUI program. Kirby
On 3-Aug-07, at 4:38 PM, kirby urner wrote:
Not sure we should call this "echoing" as what's happening is an expression is being evaluated.
No, the expression is being evaluated either way, all we are doing is turning off the default echoing of the expression result (and the side-effect of assigning the expression result to the underscore variable).
It just so happens that the eval of a quoted string returns itself whereas "some expression with %s" % "substitution" would actually evaluate to non-echo.
What the normal displayhook does is echo the result of non-None expressions, and assign those to _. What the little hack I showed did is replace that default behaviour with a no-op, a do-nothing. Whether this is a good idea or not is a different matter, but let's be clear about what's happening.
kirby@dell:~$ python ./repl.py
'THIS IS A STRING' THIS IS A STRING [x*x for x in range(10)] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] "1-2-3 %s" % "testing" 1-2-3 testing
Would one turn off all of the above, or just the first eval-print?
Not sure what you're asking. None of the evals are being changed, just the default echoing. If you then want to print you could either assign it to a variable and print that, or pass the whole expression through print. Again, I'm not saying this is a good thing, but it is more consistent with how the non-interactive runtime works.
What would be the point of a broken shell, vs explaining that Python, like many languages, features a REPL (read-eval-print loop)?
Well, it might be useful to show learners that a) this is different from what you should expect when you run your program, and b) it is not magic. For instance:
'' '' 'abc' 'abc' 'my name is %s' % 'Dethe' 'my name is Dethe' import sys def noecho(value): pass ... def echo(value): ... print value ... def echo_and_assign(value): ... print value ... global _ ... _ = value ... sys.displayhook = noecho 'abc' sys.displayhook = echo 'abc' abc _ <built-in function displayhook> sys.displayhook = echo_and_assign
Now we've re-implemented the default behaviour (modulo any edge cases it handles that I've missed). And if we want to completely get back to the default, after playing around with the displayhook, that's easy too:
sys.displayhook = sys.__displayhook__ # always returns displayhook back to system default
Or if we want something to happen every time we eval a line in the interpreter:
def enhanced_display(value): ... sys.__displayhook__(value) ... do_my_stuff() ... sys.displayhook = enhanced_display
It's all fun and games, until someone loses their ribs. ;-) --Dethe Language is what something becomes when you think in it -- Robert Bringhurst
kirby@dell:~$ python ./repl.py
'THIS IS A STRING' THIS IS A STRING [x*x for x in range(10)] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] "1-2-3 %s" % "testing" 1-2-3 testing
Would one turn off all of the above, or just the first eval-print?
Not sure what you're asking. None of the evals are being changed, just the default echoing. If you then want to print you could either
I guess it boils down to a quibble over the verb "echoing" -- a source of confusion for me. In the first eval above, the output of the eval looks a lot like the input (hence "echo"). But most expressions evaluate to a result that's different, so what you call "default echoing" I call "default printing of the results of evaluation". We're agreeing that what's happening is always an eval, and we're agreeing that the print part of REPL could be suppressed, leading to REL instead.
What would be the point of a broken shell, vs explaining that Python, like many languages, features a REPL (read-eval-print loop)?
Well, it might be useful to show learners that a) this is different from what you should expect when you run your program, and b) it is not magic. For instance:
'' '' 'abc' 'abc' 'my name is %s' % 'Dethe' 'my name is Dethe' import sys def noecho(value): pass ... def echo(value): ... print value ... def echo_and_assign(value): ... print value ... global _ ... _ = value ... sys.displayhook = noecho 'abc' sys.displayhook = echo 'abc' abc _ <built-in function displayhook> sys.displayhook = echo_and_assign
This doesn't very newbie-accessible to me.
Now we've re-implemented the default behaviour (modulo any edge cases it handles that I've missed). And if we want to completely get back to the default, after playing around with the displayhook, that's easy too:
sys.displayhook = sys.__displayhook__ # always returns displayhook back to system default
Or if we want something to happen every time we eval a line in the interpreter:
def enhanced_display(value): ... sys.__displayhook__(value) ... do_my_stuff() ... sys.displayhook = enhanced_display
It's all fun and games, until someone loses their ribs. ;-)
--Dethe
However I think these are *advanced* games. The original confusion, as I understood it, was "Python in shell mode" versus "Python running a .py file". I think we might explain that "shell mode" is a special script that implements an interactive evaluation loop, a feature we much appreciate about Python. Anyway, I'm not here to say "my way or the highway". If you or Andre want a "dead parrot mode", why not? Maybe for some students, the light will go on and all confusions will dissipate -- which I think is our shared goal. Kirby
On 3-Aug-07, at 5:35 PM, kirby urner wrote:
I guess it boils down to a quibble over the verb "echoing" -- a source of confusion for me.
I'm not attached to "echoing," that's just what it is called in the shell (and the shell is not always right). I just wanted to clarify that we the eval is still happening.
In the first eval above, the output of the eval looks a lot like the input (hence "echo").
But most expressions evaluate to a result that's different, so what you call "default echoing" I call "default printing of the results of evaluation".
Sure, that's fine.
We're agreeing that what's happening is always an eval, and we're agreeing that the print part of REPL could be suppressed, leading to REL instead.
;-)
sys.displayhook = noecho 'abc' sys.displayhook = echo 'abc' abc _ <built-in function displayhook> sys.displayhook = echo_and_assign
This doesn't very newbie-accessible to me.
No. That wouldn't be a first lesson. But maybe for a more precocious student, to keep them busy while you get the others in the class get caught up...
It's all fun and games, until someone loses their ribs. ;-)
However I think these are *advanced* games.
Agreed. I got carried away playing with it. The point is that Python has these hooks all over the place, and sometimes when we think we need to create a new version of the interpreter, all that is needed is to play with the hooks a bit.
The original confusion, as I understood it, was "Python in shell mode" versus "Python running a .py file". I think we might explain that "shell mode" is a special script that implements an interactive evaluation loop, a feature we much appreciate about Python.
Anyway, I'm not here to say "my way or the highway". If you or Andre want a "dead parrot mode", why not? Maybe for some students, the light will go on and all confusions will dissipate -- which I think is our shared goal.
Absolutely. I think we can agree to agree. --Dethe "Any idea that couldn't stand a few decades of neglect is not worth anything." --Gabriel Garcia Marquez
participants (4)
-
Andre Roberge -
Dethe Elza -
kirby urner -
Michael Tobis