This is a great discussion. While on the topic, I'd like to share a few more thoughts.
* The original discussion in comparing Python's model to close analogs from C was specifically in the context of Dave's original posting. I wholeheartedly agree with John/Kirby in that there is no reason to dig that deep with new (e.g., untainted) programmers. * For newbies, the sticky note analogy is wonderful. I carry around a pad of yellow post-it notes for the entire semester so that I can literally stick them to the objects on the classroom board. It allows us to instill a distinction between names and underlying objects and to portray the assignment semantics. * The analogy also carries over to portray the repeated assignment of a for loop variable to the container's elements. If you don't mind, take a quick peek at the figures on pages 127-132 in Ch. 4 of our book (http://esminfo.prenhall.com/computing/goldwasser/Goldwasser_ch04.pdf). You'll notice the portrayal of the names "guests" and "person" in the context of a "for person in guests:" loop. * As an aside, when picking the style for those images, we intentionally emphasize that the type-awareness is encapsulated within the state of each object (as opposed to with the label). * The analogy of sticky notes also cleanly portrays the parameter passing model. As John and I mentioned in earlier posts, it is precisely the same semantics as assignment. The formal parameter is assigned to the same object identified by the actual parameter. 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. The same mechanism is used for the return value. BTW, as for the "pass-by-value", "pass-by-reference" terminology, part of the problem is that in the programming language community, those terms aren't always used consistently. In truth, Python is more akin to the notion of "pass-by-name" as in Algol (however the pass-by-name terminology has not really caught on in general). * 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. But in the long run, it is important for students to understand that the list is a list of references (not a list of objects). The sticky note analogy does not work here because because there is no name per se to give an individual element (guests[i] is syntactically useful, but that is not a true name and the particular index is not persistent over time). To portray the underlying objects separate from the container's state, it becomes necessary to draw a picture more akin to the classic "pointer" diagrams. Yet by this point, it is helpful to do so and awareness of the underlying distinction is important. * As a final note, if students are subsequently introduced to the use of dictionaries in their own programming, you can go under the hood to give a true explanation of Python's model. Each namespace is implemented as a dictionary and the names are simply strings that are keys in the dictionary, mapped to the associated value. From within a function body, locals() command returns access to the dictionary representing the local namespace, while globals() returns the global namespace. Further lessons of name resolution can be explored as vars(x) gives the instance-level dictionary for an object x (other than for built-in primitives which do not rely on dictionaries for their internals). vars(Foo) gives the class-level dictionary for a class Foo. +----------------------------------------------- | Michael Goldwasser | Associate Professor | Dept. Mathematics and Computer Science | Saint Louis University | 220 North Grand Blvd. | St. Louis, MO 63103-2007 _______________________________________________ Edu-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/edu-sig
