[Python-3000] educational aspects of Python 3000

Tony Lownds tony at PageDNA.com
Tue Sep 12 04:27:09 CEST 2006


>> IMHO, it would be better to label the module "scripting" rather than
>> "beginnerlib" (and why append "lib" at the end of module names
>> anyway? :-)).
>> It might even contain stuff such as encoding guessing.
>>
>>>>> from scripting import raw_input, autotextfile
>
> I'm not so keen on 'scripting' as the name either, but I'm sure we can
> come up with something. Perhaps easyio, simpleio or basicio? (Not to
> be confused with vbio. :-)
>

How about simpleui? This is a user interface routine.

> I'm also not completely against revising the decision on killing
> raw_input(). While input() must definitely go, raw_input() might
> survive under a new name. Too bad calling it input() would be too
> confusing from a Python 2.x POV, and I don't want to call it
> readline() because it strips the trailing newline and raises EOF on
> error. Unless the educators can line with having to use
> readline().strip() instead of raw_input()...?
>

Javascript provides prompt, confirm, alert. They are all very useful  
as user interface
routines and would be just as useful in Python. Maybe raw_input could  
survive as "prompt".

Alternatively, here is a way to keep the "input" name with a useful  
extension to the semantics of input().
Add a "converter" argument that defaults to eval in Python 2.6.  
"eval" is deprecated as an argument in
2.7. In Python 3.0 the default gets changed to "str". The user's  
input is passed through the converter
function. Any exceptions from the converter cause input() to prompt  
the user again. Code is below.

sys.stdin.readline() doesn't use the readline library. That is a nice  
feature of the current raw_input() and
input() builtins. I don't think this feature can even be emulated  
with the current readline module.

-Tony Lownds

import sys
def input(prompt='', converter=eval):
   while 1:
     sys.stdout.write(prompt)
     sys.stdout.flush()
     line = sys.stdin.readline().rstrip('\n\r')
     try:
       return converter(line)
     except (KeyboardInterrupt, SystemExit):
       raise
     except Exception, e:
       print str(e)

if __name__ == '__main__':
   print "Result: %s" % input("Enter string:", str)
   print "Result: %d" % input("Enter integer:", int)
   print "Result: %r" % input("Enter expression:")

Here's how it looks when run:

Enter string:12
Result: 12
Enter integer:1a
invalid literal for int(): 1a
Enter integer:
invalid literal for int():
Enter integer:12
Result: 12
Enter expression:a b c
invalid syntax (line 1)
Enter expression:abc
name 'abc' is not defined
Enter expression:'abc'
Result: 'abc'





More information about the Python-3000 mailing list