[Edu-sig] python for kids...
macquigg at ece.arizona.edu
Sun Nov 11 11:48:50 CET 2007
Here is how I would explain Python variables to kids:
In Python, naming variables is like sticking labels on objects. Unlike other languages, the labels have no type. When we say x is an integer, that's just a shortcut for saying x is a name that currently refers to or identifies an integer. Like labels in the real world, these identifiers can be moved from one object to another, and there can be more than one identifier on an object.
Let's say we have two objects, a real number 4.7, and the integer 3. (In Python a "float" and an "int".) Each of these numbers has a place in the computer's memory, and a type (float or int), which was determined when the number was put in that place. If sometime later we say
>>> x = y = 3
we are telling Python we want the names x and y to both refer to this one object, the integer 3.
If we then say
>>> y = 4.7
We are moving the "label" y from the int 3, and sticking it on the float 4.7.
What do you suppose happens to x? It might depend on how the labels were originally "stuck" on the object 3. Clearly the label y was stuck to the object 3, but was the label x stuck to the same object, or was it stuck on top of the label y? Let's ask Python.
So it seems the rule is very simple. We never have to remember the order in which labels were applied to objects. They all stick to the object, not to each other. When we say x = y = z = 3, Python finds the actual object at the end of the statement, and sticks all labels to that one object. y and z are labels, not objects. You can't stick a label to a label.
Another way to look at this is to say that the labels changed, not the objects. When we moved the label y, we did not change anything at the memory location where the 3 was stored. We made a new object 4.7, and put the label y on that object.
>>> x = y = 3
10835424 # memory location where 3 is stored
10835424 # the exact same place
>>> y = 4.7
10835424 # same place as before
12218536 # a new place
The last bit might be left as an exercise, as it may overburden the short explanation. Also, I don't know if you want to introduce the id() function. It's a good way to show the reality of objects, but I see you don't even have it in your appendix.
I don't like the mailbox analogy, because it adds complexity to the mental picture (labels, mailboxes, and objects). A mailbox is really a container, like the "locker" that you talk about later with lists and tuples. A mailbox, being a solid object, is more likely to have a type, or have restrictions about what you can put in it. I would much rather think of putting labels directly on objects, not putting objects in boxes then labels on the boxes (unless you really are talking about lists or other objects that are containers).
A precise explanation of Python variables is in Chapter 4 of Martelli's "Python in a Nutshell". He uses the word "identifier" for the name, and "variable" for the actual object. You'll have to make a choice between using precise terminology, vs a more conversational, easy-reading style. I would at least put Martelli's book in the list for further reading.
"We can pull that label off and stick it on something else, or even stick the label on more than one thing." This looks like a serious error, or maybe I'm just reading it wrong.
Hope this is helpful.
More information about the Edu-sig