[Edu-sig] How does Python do Pointers?

John Posner jjposner at snet.net
Thu May 8 17:05:45 CEST 2008


> The sticky-note analogy has a flaw.  You can't stick one 
> note on top 
> of another.  When you say x = y = z, all three variables 
> now point to 
> the object originally pointed to by z.  Then when you say y = 8,
> y now points to an integer object 8, but x doesn't move with y.   
> Python's "sticky notes" have a special glue that sticks only to an 
> object, not to another variable.

IMHO it's not a flaw, but a blessing. Extending the "sticky notes" analogy
by referring to "special glue" is a very intuitive way to explain the
language feature you describe above -- assigning then reassigning "y". You
might not like that language feature, but don't place the blame on the
analogy!


>  In the classroom we take this a step further and start using a
>  second color post-it note to start laying the framework for the
>  concept of namespaces.   If we had been using yellow notes for the
>  names in the caller's context, we start using green notes for names
>  in the local namespace of the function.

Excellent! And you could also use multiple sticky-note colors (or multiple
shapes -- they exist, too) to introduce the concept of typed variables.
Switching for a moment to that other thread ("Introducing Python at our
community college"), I don't think that anyone mentioned using "assert" to
implement typed variables. Example:

    def myfunc(stringparm, listparm, intparm):
        """
        function with typed parameters,
        courtesy of "assert"
        """
        # check parameter types
        assert type(stringparm) is type('abc')
        assert type(listparm) is type(['a', 'b'])
        assert type(intparm) is type(123)

        # do the work
        print "All OK"
        return
    # ------------------------------------------
    >>> f.myfunc('a', range(3), 44)
    All OK

    >>> f.myfunc(range(3), 44, 'a')
    Traceback (most recent call last):
      File "c:\temp\<string>", line 1, in <module>
      File "c:\temp\f.py", line 6, in myfunc
        assert type(stringparm) is type('abc')
    AssertionError:

> * There is one way in which the sticky note analogy breaks down. It
>   does not accurately portray the internals of a container that
>   references other objects.  For example if you look back at our
>   figure on page 127, it gives a false impression that the contents
>   of the list are contained "within" the list.  For beginners, this
>   analogy suffices.  Also, in this particular case, the elements of
>   the list are immutable and so the distinction is less significant.

I'm not sure, but I think the sticky-notes analogy *can* be extended to
container objects (though it seems that John Zelle doesn't do this in his
textbook):

  * dictionary: an unordered set of sticky-notes
  * list: an ordered set of sticky-notes, in which the names on the notes
    can be changed dynamically by the Python interpreter -- e.g. when a new
member
    of the list is inserted.

I would agree, though, that analogies shouldn't be extended past the point
where they help students understand the actual language features.


And just to get a word in on the original question -- how does Python do
pointers -- what about iterators? From a functional viewpoint, isn't the
next() operation just like incrementing a pointer?

-John







More information about the Edu-sig mailing list