[Edu-sig] interactive vs compiled from file
kirby urner
kirby.urner at gmail.com
Fri Aug 3 21:34:29 CEST 2007
On 8/3/07, Michael Tobis <mtobis at 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 at 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
More information about the Edu-sig
mailing list