What’s the “Best Way” to Teach Python?

VPython OpenGL window inside a Jupyter Notebook

You’ll notice I put “best way” in quotes, suggesting there’s something suspicious about the idea, and there is. Also, I present my answer to a question about “teaching” Python, not learning it, so what’s the difference?

“The teaching is to the teacher, and comes back most to him” said a famous poet (Walt Whitman if you need to know), and that encapsulates a promising approach to learning anything: gear yourself to teach the subject, even if only to an imaginary audience, and your understanding will grow.

Let’s cut to the chase: the biggest improvement I’ve noticed of late, is my finally adopting Sphinx as my documentation generator of choice, and the practices that nets me.

We’ve all heard of test-driven development (TDD) by now, wherein we use tests as the rungs of a ladder, to give us traction as we “only write code to make failing tests pass” (the TDD mantra). Writing documentation is not entirely like writing tests, but has a similar ability to get out in front of the action and draw us onward and inward, deeper into whatever it is we’re working on.

Start a Sphinx project by running sphinx-quickstart and you’re on your way. Answer a few questions and then get to work on your reStructured Text documents. Start telling the story of what you’re up to, knowing you can change it later. Start to fan out, defining new pages, sketching in topic headers. You’ll have a miniature website, locally hosted, within minutes, all set up to make your code, and your concepts, shine brightly, as themed HTML.

Lesson Plan (under continuous development)

My second piece of advice is to sort out the Five Dimensions of Python as I call them, though you may call them something else. These would be:

  • keywords and core syntax, punctuation
  • the built-ins
  • special names (__ribs__)
  • Standard Library
  • 3rd Party (the jungle, or ecosystem at large)

Listing them in this order, while logical, does not imply one should master everything about Dimension 0 (where I start numbering), then proceed level by level to Dimension 4, mastering each in turn.

On the contrary, keep spiraling around and around, going deeper into each, and using your emerging knowledge of each to leverage your understanding of the others. Each time around the spiral, take in a little more, regarding topics with which you’re already in part familiar. Knowledge aggregates faster this way.

Whereas I believe the dictum that real multi-tasking is not possible, if we wish to enjoy concentration and focus, we still have a penchant for task switching. How many of us eat all of each food put in front of us, versus enjoying a variety of permutations? Exactly. Most of us would like the option to jump to a different task, coming back to the present one somewhat refreshed and ready to give it another go.

Adding Sphinx to my juggling, along with using an IDE, shell, and web browser, took me to a new place in terms of feeling productive when slacking off code generation, in favor of telling more of the story of what I’m doing.

My coding benefits from not monopolizing my attention. My documentation benefits.

Overall, I feel productive but not locked down between too few activities, not that I won’t need breaks for completely different activities.

Using Qt Designer with PyQt

That’s another tip if you’re a teacher, or a developer: learn to sleep on it, meaning learn to walk away from a problem and come back to it.

We find the same pattern holds when working out physically: give the muscle group you’ve exercised some time to recover, move on to a different exercise or machine.

Keep moving, continue varying the activity, with ample time to apparently do nothing at all (staring into empty space may be just what the internal doctor ordered).

The built-ins include some special names, which in turn provide the signature methods for such as: descriptors (data and non-data, __get__ and __set__), iterators (__next__ and __iter__), context managers (__enter__and __exit__). If you’re not used to thinking in these terms, you have some fun times ahead of you.

A more formal understanding of the grammar, which is where descriptors come in, will encourage you to discover elegant shortcuts. Even if you never plan to use metaclasses, learn what they are and how they fit in to the grand scheme of things.

Study Python as a formal language, not just as a pile of idioms one must memorize. Teach it this same way.

Finally, when it comes to 3rd Party frameworks and modules, it pays, in terms of saved time, to have your downloading ducks in a row, meaning you’re comfortable using pip install or conda and know how to separate your projects using virtualenv such that their respective dependencies don’t step on each other’s toes.

I recommend urging students use the Anaconda distribution if they’re able, as it’s just as easy to throw away as to install. Encourage a “try before you buy” attitude, even though Anaconda’s basics are all free.

Don’t demand loyalty or commitment where this hasn’t been earned. Students don’t like to be railroaded.

Anaconda is not the only way to go, and if you’re already happy with your setup, then by all means extend your installation however you wish.

Expect to add a web framework or two eventually, as well as pandas and numpy.

Make full use of Jupyter Notebooks, and you’re on your way!