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

Gregor Lingl glingl@aon.at
Tue, 18 Feb 2003 00:15:38 +0100


Arthur schrieb:

>John writes -
>...
>
>He got stuck at the equivalent of:
>  
>
>>>>from visual import *
>>>>def ball(incolor):
>>>>        
>>>>
>            sphere(color=incolor)
>  
>
>>>>ball_1=ball(color.blue)
>>>>        
>>>>
>
>#a blue sphere appears in a VPython window as expected
>
>#But he expects this command to move the center of the sphere to coords
>1,0,0
>\
>  
>
>>>>ball_1.pos=(1,0,0)
>>>>
>
># and gets
>  
>
>>>>Traceback (most recent call last):
>>>> File "<pyshell#7>", line 1, in ?
>>>> ball_1.pos=(1,0,0)
>>>> AttributeError: 'NoneType' object has no attribute 'pos'
>>>>        
>>>>
>
>He apparaently struggled with this for some time before screaming for help.
>
>When we told him:
>
>def ball(incolor):
>     return sphere(color=incolor)
>
>was what he was looking for, and why, it was to be a big revelation -
>helping to put a whole host of issues in place for him. He was thrilled.
>
>  
>
Agreeing completely with this, I'd like to stress, that this can made 
clear to the beginner, by
stating the following rule(s) - which I formulate in a (probably) 
somewhat pradoxical way:

(1) *Every function* returns a value (more exactly: a reference to an 
object), regardless
of what the programmer's intention is. If program execution doesn't  
return via a
return statement with an argument, the function returns the None - Object.

In the case above one can easily observe this by trying:

 >>> print ball(color.blue)
None

(2) *Some functions*  do something more - side-effects like printing, 
drawing, assigning...,
*if* the programmer prescribes this by writing the corresponding 
statements into the function's
body.

Gregor

>[
>It also confirmed to me that VPython is a great way to get people started
>and to illustrate basic principles, with the immediate visual feedback. I
>know I have mentioned that 100 times before, but here I can offer a concrete
>and real case, rather than one of my Zen intuitions.
>
>*I* don't need that kind of confirmation, of course -  but others seem to
>call for it. :)
> ]
>
>Art
>
>
>_______________________________________________
>Edu-sig mailing list
>Edu-sig@python.org
>http://mail.python.org/mailman/listinfo/edu-sig
>
>
>  
>