Tuesday, November 06, 2007

Pythonic Biology

from Connecting the Dots (Vilnius, Lithuania)
Included in my teaching gigs these days is a DNA, cell, creature progression, not a perfect analogy, but close enough. Here's a link to my hand-drawn handout (feel free to reuse).

At the DNA level we have primitive data structures, storehouses for information. Tuples, strings, lists, and dictionaries comprise the basic building block molecules of just about any running program.

Meshed with control structures, and provided with an ingestion port (a mouth) these stores get incorporated within functional cells. I say "functional" for a reason, as I'm talking about Python's functions here, top-level citizens in our Pythonic domain. Functions may actually eat other functions; mathematicians call this "composition" as in f(g(g(h(f(g(f(x))))))).

Finally, at the creature level, we encounter our primitive Snake, a class with a backbone, with __ribs__ like __init__. In a class definition, cells become organized into methods. We might switch to talking about internal organs at this point (again, the analogy is imprecise).

A class is a blueprint or template, it pays to remember, less so an actual object, or is an object of a different kind. At birth (initialization), a new self gets created, as in snake = Snake("Naga").

And teachers, remember to mention that "self" is not a keyword so much as a placeholder -- a different unicode string might replace it in some quirky program outside the Standard Library.


Related reading: Python in the Mathematics Curriculum (March 2004).