[Edu-sig] The fate of raw_input() in Python 3000

Toby Donaldson tjd at sfu.ca
Fri Sep 8 19:54:01 CEST 2006


> > What if instead of naming the package "teaching", it was called
> > something less offensive, like "simpleIO" or "userinput" or
> > "interactive" or "convenience"?
>
> This is a plausible way to remove the 'teaching' label. I would prefer
> 'stdin'.

'stdin' is probably meaningless to beginners. I think beginners would
prefer clear, simple names that don't require experience with Unix or
Java. :-)

> Now imagine a beginner who is looking at her very first Python program.
> The first line she will see in her first program ever will be this:
>
> from stdin import *

Yes, that would be awful. Don't do that. :-)

By using a better package name and a more explicit import you could
get something that reads better like

from userinput import raw_input

Or another approach would be to have a package named "user" or
"keyboard", so you could write things like

import keyboard

answer = keyboard.raw_input("Morbo demands an answer: ")

Another possibility, is to follow Java's Scanner class:

import java.util.Scanner;
...
Scanner sc = new Scanner(System.in);
System.out.printf("Enter the circle's radius: ");
double radius = sc.nextDouble();

A Python version could put the scanner in the main namespace, and
allow code like this:

print "Enter the circle's radius: "
radius = scanner.next_double()
print "Enter the circle's name: "
name = scanner.next_line()

I would prefer a name other than "scanner", but this gives the main
idea. This approach has a couple of nice features:

  - the code is explicit and easy to read --- you can tell just by
looking where a double is wanted and where a string is wanted
   - meaningful, custom error messages can be supplied, i.e. if the
next input is not a valid double, then next_double can say it expected
a double but found none, next_int could say it expected an int, etc.;
this is preferable to the inconsistent behaviour of input:

>>> input("Enter radius: ")
Enter radius: 4
4
>>> input("Enter radius: ")
Enter radius: four
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<string>", line 0, in ?
NameError: name 'four' is not defined
>>> input("Enter radius: ")
Enter radius: for
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<string>", line 1
    for
     ^
SyntaxError: unexpected EOF while parsing

> This person and her instructor will have to deal with modules and import
> FIRST, although the student is not likely to develop multi-modular
> programs at the very beginning.

In the Python course I teach, students need to import .py files they
write into the interpreter (for testing), so they very soon need to
write statements like

>>> from lab2 import *

Also, the very first statement students see is:

>>> import turtle

It is not hard to explain this in a way so that beginners can
understand it well enough to use it. More details and subtleties of
modules and imports come later, as needed.

> I wonder how is the instructor going to tell her what is the meaning of
> 'from stdin import *'? I guess, in the same way instructors tell what
> 'public static void main (...) is'. In this way we are going to the Java
> nonsense (nonsense from the perspective of teaching beginners, of
> course, otherwise Java is a wonderful language).

Its kind of like the nonsense explanations you see for the Python
"print" statement and "+" operator. :-)

Teaching is filled with IOUs. We often use things before we completely
understand them.

Toby
-- 
Dr. Toby Donaldson
School of Computing Science
Simon Fraser University (Surrey)


More information about the Edu-sig mailing list