On a whim, I tried converting one module of Python code in my Django app to Pyrex, to see what kind of performance benefits I'd get. So far, I haven't actually succeeded in getting the C code to compile. Changing the Python code so that Pyrex could handle it was pretty straightforward, however, and it also led to a small revelation.
Pyrex can't handle some Python features such as list comprehensions, generators or closures. I had a few list comprehensions (easily changed), no generators and just one closure (I wrote the code a while ago — it would probably have more closures if I wrote it today). Rewriting a closure is actually pretty straightforward. Instead of something like this:
def foo(x): def bar(y): return something_with(x, y) return bar
class Bar: def __init__(self, x): self.x = x def __call__(y): return something_with(self.x, y) def foo(x): refurn Bar(x)
This was the first time I had to explicitly perform the transformation on a real bit of code, and it made it blindingly clear that classes are human- compiled closures.
Of course, some people would object that we would use classes anyway, and in different ways to the above code, because they are part of object-oriented programming. Due to increasingly negative experiences of OOP at work, and a taste of the absolutely nightmarish mess it can make of a large software project, I've become very disillusioned with that programming paradigm, and find myself believing more and more that state is the root of all evil. So what I really want is closures and functional programming — not classes and OOP at all. (Of course, that's not to say that all OOP is hopeless — a lot works well. But I'm less and less convinced about it in general).
This means, of course, that Paul Graham is right, again. That's the really annoying thing about smug lisp weenies — they're always right.
Well, if you can't beat 'em, join 'em! I think the first functional programming language I'm going to learn is Haskell — I've looked at it a little bit, and it seems wonderfully elegant, and I want a pure FP language to ensure that I really learn the functional style. Once I've got my head round Haskell and monads, and I'm craving for macros, I'll take the red pill and join the Rubber Duck brigade. When I (hopefully) achieve enlightenment, I'll try not to be too smug :-)