Guide to the python interp. source?

Terry Reedy tjreedy at udel.edu
Sun Jul 28 18:09:18 EDT 2002


"Tim Gahnström /Bladerman" <tim at bladerman.com> wrote in message
news:bxX09.1437$HY3.398059 at newsc.telia.net...
> ask a complete novice what he think this peace of code will do
>
> #Here is the program
> x=3
> inc(x)
> print x
>
> #Bellow is al the function deffinitions
> def inc(x):
>     x= x+1

Your example is deceptive and biased because you use the same name for
the global and local vars, which is known to be confusing to complete
novices, let alone those with more experience.  They will tend to
think that they are the same thing and give the wrong answer on the
basis of that misconception.  Much better is

def inc(temp):
  temp = temp+1

> You might have to tell him about assignment but not much else for
him to
> understand that this program will print 4

I tried your experiment with my wife, who is not even a novice w/r/t
computer programming.  She first refused to answer.  When she
(mis)interpreted 'inc(x)' as acting like a calculator key press, with
an implicit 'x=' prefixed, she thought '4'.  When I 'inlined' the
function call and rewrote the program as

x = 3
t = x
t = t+1
print x

she immediately said '3'.

To get 4 correctly (rather than on the basis of misunderstanding), one
would have to inline inc (mentally or actually) as something like

x = 3
t = &x
*t = *t+1
print x

but no novice would think of this and few would grok it.  (Those who
do are, of course, passing beyond novicehood.)

If you want to use Python as if it were a calculator, write your
functions to return a value, run in interactive mode, and use '_'
instead of 'x' as the name of the default and automatically displayed
variable.  All expressions get an implied '_=' prefix just like the
implied 'x=' prefix of calculators:

>>> def inc(t): return t+1
...
>>> 3
3
>>> inc(_)
4

Terry J. Reedy







More information about the Python-list mailing list