[Edu-sig] interactive vs compiled from file

Andre Roberge andre.roberge at gmail.com
Sun Aug 5 18:27:05 CEST 2007


On 8/5/07, John Posner <jjposner at snet.net> wrote:
>
> Nice discussion! Here's my suggestion: if the user types an expression
> that
> returns a value,



This is a fantastic idea to introduce the various types.   I prefer this
over trying to do
type('hello') explicitly at the prompt.

Your implementation works very well too - and not only in IDLE.

André


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
>
>
> _______________________________________________
> Edu-sig mailing list
> Edu-sig at python.org
> http://mail.python.org/mailman/listinfo/edu-sig
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/edu-sig/attachments/20070805/d8ca63f9/attachment.htm 


More information about the Edu-sig mailing list