-4

When teaching students Python lists (which are more like arrays, not linked-lists) I'd like to use metaphor of list being a collection of boxes put in row.

But this doesn't work well:

  • indexes are important to be numbers, but in my example indexes should actually be counted, and it is not obvious that "counted" and "index" are same. At least, from performance PoV indexes are not "counted"
  • and I can't name boxes with increasing numbers, as this is plain wrong, indexes are not names of boxes, they are property of list itself. For example, list slice or splice should change names of boxes, which isn't realistic
  • the boxes metaphor fails for list of lists

A slightly better metaphor for list of integers is:

  • list is a train with wagons of same length
  • and indexes are special signs on railway platform
  • the train stays near platform in such way, that the connection between train and first wagon is next to sign with number "0", index 0
  • other signs on platform stays near other connections in an increasing order
  • the train head and railway platform together are a list, the wagons are elements of a list
  • any slice or splice of train wagons is done with train head staying where it stays and extending/squashing all remaining wagons to be connected.

This is slightly more correct, but fails for list of lists (or list of objects). Are there better metaphors?

danbst
  • 93
  • 1
    Might be a better question on [cseducators.se]? With my C programmer hat on, I find the "boxes have names" metaphor most correct, where names are addresses, and list[i] is just a shortcut for array_offset + i * box_size. Splice operations do move contents to different boxes/addresses. Your train metaphor suggests sequential memory access, but RAM allows random access. Your metaphor only struggles with lists of lists because you suggest that the waggons contain the object rather than a pointer to the object. Once you've established basic intuition, telling the truth might be simpler. – amon Oct 18 '20 at 05:57
  • 1
    Metaphors are sometimes helpful to explain things to laymen, but if I am teaching students, I try not to overstress this "tool", this has a huge potential to bring in extra confusion. In the end, a list is an abstract data type which is precisely defined by the operations which are implemented in the specific language or framework - which metaphors won't tell. A concept like lists is best explained by showing students some problems which can be solved by them, and even more by letting them solve some problems on their own. – Doc Brown Oct 18 '20 at 07:10
  • @amon yes, this is more cs edu! How couldn't I notice that site. But for your example, "boxes with names" is incorrect, as indexes are not names, but addresses are names. – danbst Oct 18 '20 at 07:26
  • @DocBrown I don't share the mindset that solving problems make students understand things, more often it makes students struggle and dislike the subject. And it is best to imprint proper metaphor first, which has ties to real world, and only after "tell the truth". This all depends on prior student level though – danbst Oct 18 '20 at 07:30
  • 1
    "I don't share the mindset that solving problems make students understand things" - well, then I am very lucky that you were none of my teachers. And I did not write that metaphors are useless in teaching, I just wrote I would not overstress this tool (and hence not overthink this). – Doc Brown Oct 18 '20 at 07:36
  • I find the best lesson is that all metaphors are lies. Now see if you can spot the lie. – candied_orange Oct 18 '20 at 16:53
  • @candied_orange for newcomers, who are not familiar with rule-based constructions, metaphors are the way to smooth connect real and computing worlds. I really hope good metaphors (i.e. isomorphisms) exist – danbst Oct 18 '20 at 17:53
  • 1
    @danbst I didn’t say don’t use metaphors. I’m saying be honest about them. – candied_orange Oct 18 '20 at 18:28
  • From the point of the view of the list's interface, indexes very much are a way to name the boxes, even though they aren't the property of the boxes themselves. They are a form of indirection. If you want to emphasize that specific point, then you could say something like: the boxes represent memory, and each list simply puts a numbered sticker next to a box - the stickers themselves are then the list (and boxes are just something in the background). A slice would then produce a different object, with a different set of stickers that number the same boxes differently. – Filip Milovanović Oct 18 '20 at 22:35
  • But most of the time we don't need to think in that way; and our ability not to think that way is important, as abstraction (and jumping levels of abstraction) is an important skill in programming. – Filip Milovanović Oct 18 '20 at 22:36
  • Okay, I found a good metaphor. Lists are like tape rulers, and values are attached between cm/inch markings either directly (numbers) or via tentacles (by-ref). And rules are that two can’t occupy same place and should be sequential. You can make “holes” by attaching None tentacle. List compactness may be implemented by placing tape 45 degrees to horizon, so gravity will squash holes after inplace splice – danbst Oct 19 '20 at 06:39
  • 1
    @danbst - I thought you were concerned about making it realistic, and here you are, talking about tentacles and gravity :D This can perhaps work as a nice metaphor to depict indirection if it's accompanied by something visual (pictures, animations), because without them it's too elaborate IMO, and students who are new to this material can't separate relevant vs superfluous details, so they'll get confused without some hand-holding. – Filip Milovanović Oct 19 '20 at 13:35
  • @FilipMilovanović indeed, I've searched for a visual-able metaphor! Tentacles, tapes and gravity are visualizable, much better than addresses, references and indexes. P.S. In case you still read this, I believe the biggest problem with teaching CS is that students don't visualize things by default, even when you ask them. Even more, they don't try to think and make inferences, often even if you ask. By using real visual metaphors I plan to enforce imagery subsystem, which will make needs for inferences. So I wanted something both correct and visualizable – danbst Oct 19 '20 at 16:38

1 Answers1

1

Any metaphor for Python's data model involving boxes is going to be problematic. Instead, I like strings. As in, physical strings or ropes, connecting a name to a value.

A list is then simply a sequence of strings, each of which is connected to a value.

  • It doesn't address the O(1) access, but I'm not sure how a visual metaphor can explain that.
  • You can say "you're allowed to add or remove any number of strings, as long as each string points to a value" regarding in-place modification of a list, while I would explain slicing and concatenation as simply creating a new list whose strings point to the same values as the original list.
  • Lists of lists (even recursive lists like l=[];l.append(l)) are easily explained by saying the value a string points to is allowed to be a list.

The article Python Names and Values by Ned Batchelder contains some graphics that I think are relevant to this metaphor (it's also a good and short read).

Jasmijn
  • 1,829
  • 11
  • 16