[Edu-sig] interactive vs compiled from file

John Posner jjposner at snet.net
Sun Aug 5 17:59:54 CEST 2007


Nice discussion! Here's my suggestion: if the user types an expression that
returns a value, 
have IDLE echo the return value annotated with its type, and with "<<<" as a
prefix. Reversing the direction of the angle brackets is a neat (IMHO) way
to remind the user of the concept of "return value".

Examples:

############################################# set it up

>>> import sys
>>> sys.displayhook = show_expression_value

############################################# show how it works

>>> 23 + 45 - 31
<<< 37              (INTEGER expression value)

>>> 2/3.0
<<< 0.67            (FLOAT expression value)

>>> 'hi' + 'there'
<<< 'hithere'       (STRING expression value)

>>> def f(inp):
        return inp

>>> f('func arg')
<<< 'func arg'      (STRING expression value)

>>> f(2 + 3 + 4)
<<< 9               (INTEGER expression value)

############################################# quiet if return value is None

>>> a = 2 + 3 + 4
>>> a = f(4.56789)


Here's my implementation:

def show_expression_value(val):
    """
    Show expression value or function return value in IDLE.
    Deploy this function with: sys.displayhook = show_expression_value
    """
    if type(val) is type("hello"):
        displayme = "'%s'"  % val
        print "<<< %-15s (STRING expression value)" % displayme
    elif type(val) is type(1):
        displayme = "%d" % val
        print "<<< %-15s (INTEGER expression value)" % displayme
    elif type(val) is type(1.0):
        displayme = "%.2f" % val
        print "<<< %-15s (FLOAT expression value)" % displayme

    # TO DO: more "elif"s for other value types




More information about the Edu-sig mailing list