I teach about 10 different courses to companies around the world, but my favorite remains “Python for non-programmers.” Participants in this course are typically network and system administrators, support engineers, and managers who want to learn some programming skills, but don’t see themselves as programmers. Moreover, many of them took a programming course back when they were university students, and were so horrified, overwhelmed, and frustrated that they gave up. Perhaps they’re still working for a high-tech company, but they have tried to avoid programming.
But jobs increasingly require some knowledge of programming, and Python is a perfect language with which to start: The syntax is consistent, and the number of things you need to learn is relatively small in order to get up and running.
But that doesn’t mean that there’s nothing to learn. And one of the hardest ideas for people to learn is that of variables. Sure, people know about variables from when they learned algebra — but variables in programming languages aren’t exactly the same thing, even if there are similarities.
For years, I struggled to explain variables: I used the mailbox metaphor (which I learned from the wonderful “Computer Science, Logo Style“). But the mailbox model doesn’t really fit Python, so I’d end up saying, “Actually, I lied to you yesterday. Variables are actually references.” Which didn’t do much to clear things up. And when we started to talk about lists of lists, it wasn’t clear how much my explanations really helped.
Finally, I hit upon a metaphor that seems to resonate with people: Variables are pronouns. This has several advantages:
- Everyone knows what pronouns are. So it’s easy to understand how we might use them, and how they save us time. After all, you it’s far easier to say “he” or “him,” rather than “Rufus Xavier Sarsaparilla.”
- The notion of references emerges naturally from this description. No longer do I introduce the mailbox model, and then point out how it doesn’t really work, given how assignment in Python works.
- It becomes obvious that a variable refers to the object to which it was most recently assigned, much as “she” refers to the most recent female to whom we referred. Just as “he” and “she” can only refer to a single object at a time, so too can variables only refer to a single object at a time.
- It also follows that while a variable (pronoun) can only refer to a single object, an object might be referred to by several pronouns.
No metaphor is perfect, and it’s still tough for many people to wrap their heads around the idea of variables when they’re programming for the first time. But this model seems to have had the greatest success so far. If you teach Python programming, then give it a whirl, and let me know if it seems to help!