Porting Objective-C to Python

I went to the Vancouver Python Conference last weekend. I'll have more to say about that later, but for now I'll just say it was AWESOME.

There was a session about PyObjC, and a session about PyGame. The speaker of the latter session mentioned he prefers to use PyObjC but often uses PyGame because it runs on more platforms.

I was inspired to take another look at a board game I wrote in Objective-C / Cocoa 3.5 years ago (and improved 2.5 years ago) and port it to Python.

I have ported one of the classes so far, the one that represents the game board. Not that lines of code is a tell-all metric, but it was interesting that it went from 165 lines to 124 lines. Most of the savings was probably in not having to declare local variables before using them, and not having lines just for end braces. I did basically a straight port, seldom did I use uniquely pythonic constructs.

It was a little disturbing removing the return types from methods and the types from the instance variables, but it was fun removing all of the alloc, retain, autorelease and objectAtIndex calls.

The exercise so far has reminded me how good Objective-C is. It's a dynamic language that allows static typing. It's a nice balance. If it had garbage collection and Python-style operator overloading, it would be even more brilliant.

It will be fun once I get past the model layer (which I'm making pure Python, not using Foundation) and onto the Controller layer. I'm planning to convert the NIBs to use Cocoa Bindings (KVB). I imagine the overall line-count of the Python/PyObjC/KVB version will be dramatically smaller than the current version.

I doubt I'll find the time, but after I'm done I could make a PyGame version that ran on other platforms.

Written on August 3, 2004