[Edu-sig] interactive vs compiled from file
kirby urner
kirby.urner at gmail.com
Sat Aug 4 02:35:45 CEST 2007
> > kirby at 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
More information about the Edu-sig
mailing list