Wednesday, December 18, 2013

I'm Using Python Now

I have a confession. All these years I've been evangelizing strongly statically typed languages, I've been going home at night and using Python. Not all the time. It's more like the hungry vegetarian graduate student who comes to the free lunch and discovers only meat dishes. Sometimes you have to do what you have to do.

It started with small things here and there. Because of its libraries, Python has been my go-to language for web scraping jobs. Because of its relative concision to Java and its relative principles to PHP, Python has become my go-to language for web backend programming. And as the web is getting fancier and fancier, I've been doing more of this. As the web has been getting fancier, my research has also involved more of this. Web backend programming. And also Python.

It's true. I'm now doing research programming in Python*. For my PhD, I've been developing Jeeves, a new programming language (and soon-to-be web framework) for automatically enforcing privacy policies. Jeeves makes the programmer's life easier by making the language runtime responsible for keeping track of the privacy policies policies. As this approach happens completely at run-time rather than compile-time right now, it is a great fit for embedding into Python, which does most things dynamically (at run time). And so we switched from the Scala implementation we had been maintaining for a couple of years to Python.

The initial reason for switching to Python was that it is my favorite popular language for web backends. Popularity matters: a web framework is a big piece of software. Lots of people using it usually corresponds to lots of people developing and maintaining it, as well as more documentation for how to use it. I had found both of these to be issues with Scala web frameworks: the two popular Scala web frameworks, Lift and Scalatra, have learning curves that are quite steep. The people-power behind development, maintenance, and documentation also means that it is likely that lots of people will continue using it.

Another reason for the switch was that Python is what the (MIT undergraduate) kids are learning these days. I had thought that I could push Scala upon undergraduate research assistants using the force of my charisma, but this is harder than it might sound. It can take upwards of half a semester to teach even a motivated, bright undergraduate Scala. And given that undergraduates tend to be around for about a semester, that does not leave much productive work time.

After switching to Python, I learned that playing with language features in Python is much faster than in Scala. Much of the time spent coding in the Scala implementation involved an intricate dance with Scala's type system. While it's thrilling never knowing if your new tricks will allow you to save yourself from writing type-casing boilerplate, it takes a lot of time to convince the Scala type-checker that you're doing something reasonable in all cases. Sure, people might object that writing in Python is like swimming without a life jacket or something, but if you're an experienced swimmer trying out different moves in a small body of water, a life jacket is just going to hinder you. Same effect for experienced programmers prototyping a research language...

Something else that I've discovered is that people outside of programming languages seem much more excited about using my language when I tell them it is embedded in Python. These people include computational biologists, computer scientists working in areas other than programming languages (systems; the web), and undergraduates who are trying to work on our research project. A computational biochemist I spoke to who has been using logic programming was quite excited that we were embedding our language in Python. There are already many biological modelling toolkits written in Python, he said, so he could easily envision people picking up our tool.

I'm not saying everyone should use Python instead of Scala. I still stand by everything I say in my other blog post praising Scala: Scala is a less pretty version of the ML family of languages that is potentially way more usable because of its interoperability with Java. If you're trying to do quick-and-dirty language prototyping or trying to build a web framework, however, Scala may not be the tool for the job.

I know I'm not going to be able to check my Python programs before I run them. And I know I'm never going to be able to run my programs super fast. But I've looked into the tradeoff space and made my choice. Good thing Python is for girls.

* In the past few decades, the fashion has been to conduct programming languages research using statically typed languages with strong type systems. The idea is that since we have the compute power to check our programs before our run them, it is irresponsible not to. Thus using something like Python is considered somewhat scandalous.