Learning Python

I took a 3-hour tutorial on basic Python at OSCON this year, partly to see what it was like from the perspective of a neophyte because I’ve been teaching a lot of newbies to code lately. Plus I just like Python, potentially I could come to love Python… which doesn’t of course mean that I don’t have some issues and puzzlements about that community.

I have to say I can’t approve of the way Python is taught, which is invariably feature-based rather than task-based. Our instructor did the typical Python thing, which is to start babbling on about lists-tuples-dicts, demonstrate slices, move on to closures, deliver a little lecture on OOP, and finally try to explain the whole Python 3 thing. That’s all very very cool, but let me put it this way: think of a case from your entire career in which you needed to use a tuple as the key to a dictionary item. Ops Boy came up with one (geocoding), but that’s not exactly a daily task for most programmers. I happen to believe that the vast majority of humans — particularly women — learn faster if given concrete, task-based instruction with a goodly amount of repetition using realistic examples from meaningful domains.

I think this focus on “cool rather than useful features of Python” generally comes out of what I see as their biggest weakness as a community: lack of focus, exacerbated by small size. Check out the Python.org statement of what Python is good at: everything from humongous web frameworks to scientific programming to games to new ideas in sockets. PHP, in contrast, knew from Day One what it wanted to be the best at when it grew up, and generally PHP teaching tends to be quite concrete and task-based. From what I’ve seen of Perl culture, it’s even more about getting shit done without regard for adorable flourishes. I get the feeling that there’s an awfully large body of knowledge that 100% of PHP users will want to know; but for Python there might be 5 disparate topics, each of which is beloved by 20% of the community. It’s hard to grow the entire team like that.

At this juncture you probably want to point out that Java has a universal focus with seemingly more projects and libraries than there are grains of sand on the beach… but they are also probably the biggest community of all time, with massive corporate resources behind them. Pythons, if you’ll permit me a gross generalization albeit one based on personal experience, tend to be proud individualists who have difficulty accomodating the herdthink that is a necessary part of organizing resources at scale. Under those circumstances, it’s difficult to grow a community big enough to be great at so many things; and it’s disastrous to follow Java’s path towards imperial overstretch. I’ve also come to believe that PHP was lucky in never having a single Benevolent Dictator type, but instead having multiple leaders who were good at different things and had different interests.

It’s possible that deep down Pythons don’t care about being popular — and that would be cool if it were true. But it would be a shame if they actually want to be understood and loved, and just can’t explain themselves to newbies. I can only reiterate that task-based rather than feature-based thinking is the way to go. If you can’t explain the 10 things that everyone will want to do with Python rather than another programming language, and the 10 questions everyone will have about Python, then you probably have a bigger problem than just a lack of publicity.

3 thoughts on “Learning Python”

  1. “think of a case from your entire career in which you needed to use a tuple as the key to a dictionary item.”

    Well, a few months ago, I wanted to take a bunch of database rows containing a groupname, a rating, and a year, and turn them into an HTML table whose rows were years and whose columns were groupnames. So I said:

    groupnames = list(set(datum[‘groupname’] for datum in data))
    years = list(set(datum[‘year’] for datum in data))
    hash = dict(((datum[‘groupname’], datum[‘year’]), datum[‘rating’])
    for datum in data)
    rows = [dict(year=year,
    ratings=[hash.get((group, year)) for group in groupnames])
    for year in years]

    Maybe I overlooked a simpler (or clearer) way of doing that; let me know if you think of one.

    Other uses I see: in Roman V. Kiseliov’s ImportXLS module, which unsurprisingly uses a dict indexed by (row, column) tuples to store the data it’s parsing out of an Excel spreadsheet file; in the Python debugger, breakpoints are kept in a dict indexed by (filename, line) tuples; the cookielib module in the standard library keeps cookies in a dict indexed by (domain, path, name) tuples; the filecmp module in the standard library has a function cmp() to compare two files, which memoizes its results in, unsurprisingly, a dict indexed by tuples of its arguments; the gettext module in the standard library stores its catalog of localized text strings in a dict indexed by some kind of (msgid, index) tuple; and another half-dozen or so modules in the standard library use it. A way of explaining it to SQL folks is that basically it’s useful any time you’d be doing a GROUP BY on a multi-field key, or having a multi-field primary key in a table.

    I thought it was a feature that I actually used a lot more than I apparently do.

    I agree that it’s much more interesting to explain a programming language feature in terms of what it’s good for rather than in terms of how it works. I’m not sure it’s necessarily a good idea to design the language that way, as well as teaching it that way; a general-purpose programming language like C, JavaScript, Scheme, Ruby, or Python is necessarily applicable to a wide variety of problems, and it gets some benefits from that.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s