Wilcox Development Solutions Blog

Python - a readable snake

June 18, 2004

One of the neat things about Python is that it’s very readable - even when you’re learning a module you’ve never seen before. Take this code, from the Pycurl project:


import sys import pycurl

class Test: def init(self): self.contents = ”

def body_callback(self, buf): self.contents = self.contents + buf

print >>sys.stderr, ‘Testing’, pycurl.version

t = Test() c = pycurl.Curl() c.setopt(c.URL, ’http://curl.haxx.se/dev/’) c.setopt(c.WRITEFUNCTION, t.body_callback) c.setopt(c.HTTPHEADER, [“I-am-a-silly-programmer: yes indeed you are”, “User-Agent: Python interface for libcURL”]) c.perform() c.close()

print t.contents


A 2 minute look at this example and you know what’s going on - make a new curl object, go to the following url, and when we’re downloading data from that location, use a small class as a data store. When we’re done, print the contents of our data store.

It just made sense, even if it feels very C like (which makes sense - it’s based on a C API after all.

Plus, Python modules are usually a lot easier to install than C++ code (especially when make gives cryptic error messages - gcc: no input files or some such.) when trying to compile them.)