Python Epistemology: PyCon Taiwan 2013

As I explained, the title is more grandiose than accurate. In general, epistemology is the theory of knowledge: how we know what we think we know, etc. This talk is mostly about what Python has taught me about programming, and how programming in Python has changed the way I learn and the way I think.

About programming, I wrote:

I gave an example using the Counter data structure to check for anagrams:

This is a nice solution because it is concise and demonstrably correct, but I suggested that one limitation is that it does not extend easily to handle "The Scrabble Problem": given a set of tiles, check to see whether you can spell a given word.

We can define a new class, called Multiset, that extends Counter and provides

:

About programming, I wrote:

**Programming is not about translating a well-known solution into code, it is about discovering solutions by writing code, and then creating the language to express them.**

I gave an example using the Counter data structure to check for anagrams:

**from collections import Counter**

**def is_anagram(word1, word2):**

return Counter(word1) ==

**Counter(word2)**This is a nice solution because it is concise and demonstrably correct, but I suggested that one limitation is that it does not extend easily to handle "The Scrabble Problem": given a set of tiles, check to see whether you can spell a given word.

We can define a new class, called Multiset, that extends Counter and provides

**is_subset**

**class Multiset(Counter):**

"""A set with repeated elements."""

def is_subset(self, other):

"""A set with repeated elements."""

def is_subset(self, other):

for char, count in self.items():

if other[char] < count:

return False

return True

for char, count in self.items():

if other[char] < count:

return False

return True

Now we can write

**can_spell**concisely:**def can_spell(word, tiles):**

return Multiset(word).is_subset(Multiset(tiles))

return Multiset(word).is_subset(Multiset(tiles))

I summarized by quoting Paul Graham:

**"... you don't just write your program down toward the language, you also build the language up toward your program.**

Paul Graham, "Programming Bottom Up," 1993.

"In the end your program will look as if the language had been designed for it. And ... you end up with code which is clear, small, and efficient."

Paul Graham, "Programming Bottom Up," 1993.

In the second half of the talk, I suggested that Python and other modern programming languages provide a new approach to solving problems. Traditionally, we tend to think and explore using natural language, do analysis and solve problems using mathematical notation, and then translate solutions from math notation into programming languages.

In some sense, we are always doing two translations, from natural language to math and from math to a computer program. With the previous generation of programming languages, this process was probably necessary (for reasons I explained), but I claim that it is less necessary now, and that it might be possible and advantageous to skip the intermediate mathematics and do analysis and problem-solving directly in programming languages.

After the talk, I got two interesting questions by email. Yung-Cheng Lin suggested that although programming languages are more precise than natural language, they might not be sufficiently precise to replace mathematical notation, and he asked if I think that using programming to teach mathematical concepts might cause misunderstandings for students.

I replied:

*I understand what you mean when you say that programming languages are less rigorous that mathematical notation. I think many people have the same impression, but I wonder if it is a real difference or a bias we have.*

*I would argue that programming languages and math notation are similar in the sense that they are both formal languages designed by people to express particular ideas concisely and precisely.*

*There are some kinds of work that are easier to do in math notation, like algebraic manipulation, but other kinds of work that are easier in programming languages, like specifying computations, especially computations with state.*

*You asked if there is a danger that students might misunderstand mathematical ideas if they come to them through programming, rather than mathematical instruction. I'm sure it's possible, but I don't think the danger is specific to the programming approach.*

*And on the other side, I think a computational approach to mathematical topics creates opportunities for deeper understanding by running experiments, and (as I said in the talk) by getting your ideas out of your head and into a program so that, by debugging the program, you are also debugging your own understanding.*

In response to some of my comments about pseudocode, A. T. Cheng wrote:

*When we do algorithms or pseudocodes in the traditional way, we used to figure out the time complexity at the same time. But the Python examples you showed us, it seems not so easy to learn the time complexity in the first place. So, does it mean that when we think Python, we don't really care about the time complexity that much?*

I replied:

*You are right that it can be more difficult to analyze a Python program; you have to know a lot about how the Python data structures are implemented. And there are some gotchas; for example, it takes constant time to add elements to the end of a list, but linear time to add elements in the beginning or the middle.*

*It would be better if Python made these performance characteristics part of the interface, but they are not. In fact, some implementations have changed over time; for example, the += operator on lists used to create a new list. Now it is equivalent to append.*

Thanks to both of my correspondents for these questions (and for permission to quote them). And thanks to the organizers of PyCon Taiwan, especially Albert Chun-Chieh Huang, for inviting me to speak. I really enjoyed it.

That's a really cool talk. Please let us know about the parrot!

ReplyDeleteThanks for asking!

DeleteIn general, the cover designers at O'Reilly don't take a lot of input from authors. They pick the animal -- you don't.

But they showed me a draft cover for Think Python, and the animal was... wait for it... a python.

I wrote back and said that would be fine, but just so you know, Python is named after Monty Python, not the snake (which is why my book is full of Monty Python references). So every time someone puts a python on the cover, they're not getting the joke.

They wrote back and said fine, so what's a good Monty Python animal? I suggested a dead parrot (see http://en.wikipedia.org/wiki/Dead_Parrot_sketch), ideally a Norwegian Blue.

They wrote back and said (1) there is no such thing as a Norwegian Blue parrot, and (2) they are not putting a dead parrot on the cover.

So the compromise solution is a live Carolina parrot.

Well played!

DeleteIncredible insights into the way we think, and how Python programming provides a new medium for expressing (and experimenting) with executable thoughts.

ReplyDeleteThank you for your many contributions to open source, and to the betterment of others.

Sincerely,

Abe Usher

Thanks for your kind words!

Delete