[Edu-sig] Interactive interpreter: expressions vs. statements

Kirby Urner urnerk@qwest.net
Sat, 15 Feb 2003 13:54:08 -0800


>
>I'd like to know whether any of you have encountered these
>problems before, how frequently they turn up, and how you have
>dealt with them.
>
>Thanks!
>
>(I have an idea in mind, but i'd like to hear about your
>experiences first.)
>
>
>-- ?!ng

I think you ask very good questions.

It's OK to use 'print' in place of 'return' if the audience
for the output is an eyeball looking at stdout.  You can actually
produce fancy, formatted output this way, and maybe that's all
you need.

But Python is somewhat unaware of this window, just as actors
on stage or in a movie are supposedly unaware of the audience
(which they aren't, but that's the illusion).  From Python's
point of view, there's this pipe marked 'stdout' and it knows
under what circumstances to stuff something into that pipe.
Where it goes from there depends on the configuration --
usually it goes to some terminal window.

But just because an actor says something out loud (as in
a soliloquy), if no other actor overhears, then only the
audience is privy.  An actor much actually talk to another
actor if that other actor is to "get it".  That's when we
use 'return' with assignment, as in

 >>> a = f(x)

Now 'a' knows the result of the calculation (even if we,
the audience, do not -- because it was "whispered" through
the = sign -- which even *looks* like a little pipe).

======
Useful passage from the docs (sys module):stdin stdout stderr
File objects corresponding to the interpreter's standard input,
output and error streams. stdin is used for all interpreter input
except for scripts but including calls to input() and raw_input() .
stdout is used for the output of print and expression statements
and for the prompts of input() and raw_input(). The interpreter's
own prompts and (almost all of) its error messages go to stderr.
stdout and stderr needn't be built-in file objects: any object
is acceptable as long as it has a write() method that takes a
string argument. (Changing these objects doesn't affect the
standard I/O streams of processes executed by os.popen(),
os.system() or the exec*() family of functions in the <module-os.htm>os 
module.)
=====

You could show students how it's possible to redirect output from the
print statement, from the terminal to a file, simply by changing the
value of stdout.

  Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
  Type "copyright", "credits" or "license" for more information.
  IDLE 0.8 -- press F1 for help

  >>> import sys
  >>> sys.stdout
  <PyShell.PseudoFile instance at 0x00A0D190>
  >>> newout = open("somefile.txt","w")
  >>> sys.stdout = newout
  >>> print 'The rain in spain stays mainly in the plain'
  >>> newout.close()

Note that output from the print statement will have gone to a text file,
which may now be examined in a text editor.

Note also the PyShell.PseudoFile reference.  Again, from Python's point
of view, stdout is just a pipe to someplace -- it's just like writing
to a file, but in this case the "file" is a pseudo-file, i.e. is really
the terminal window itself (which scrolls, just like paper, i.e. the
metaphor of "file" is intact, given the terminal window looks at a
scrollable document).

Kirby