Introduction to PyObjC (Orestis Markou)

Tags: europython2009, europython

If you fire up “xcode”, you get the option to create a python cocoa project. This sets up a basic structure for your Mac python application. Inside xcode, you can draw the user interface in the “Interface Builder” with input fields, labels, buttons and so on.

OSX uses a model/view/controller (MVC) architecture for the applications, so you need to write a controller now to react to events in your fresh user interface. The controller is a python file. You normally have placeholders for the events and actions and link them with xcode (drag/drop) to the user interface, though you also can do it with python.

In case you wonder about all the prefixed names (NSSomething, NSAnother, IBSomething): objective-C doesn’t have namespaces like python, so everything needs a prefix (NS for NextStep, IB for interface builder, etc.)

When you do this, you program with Cocoa. Cocoa is build with objective-C. Objective-C is open, but it is basically useless without Cocoa. Cocoa is a set of classes to create OSX (and iphone) programs. Strong MVC support. There are lots of libraries (webkit and so).

PyObjC is a bridge that allows you to interact with Cocoa classes. Three warnings:

  • You must learn Cocoa.
  • You will curse all other UI toolkits.
  • You can’t develop for the iPhone.

Your python NSObject subclasses must adhere to Cocoa conventions as they’re just proxied objects. This means that they’re not really pythonic. You simply have to follow the cocoa conventions. The good news is that after a while you get used to it. A handy feature (as objective-C uses getters and setters methods that don’t look that well in python) is the key/value support which translates pretty well to python properties (so something that calls a getter/setter behind the scenes).

Conclusion: it is not as easy as you’d ideally want. But it is straightforward once you get used to it. And, to repeat, you need to learn Cocoa and objective-C anyway. It is possible to write a nicer wrapper, such as Ruby has apparently done. But it takes a lot of work. And there’s something to be said for the current straightforward system.

Two handy links: http://www.cocoabuilder.com and http://www.stanford.edu/class/cs193e

Comments on the iphone

I exchanged some emails with Mikko who said “actually you can develop for iPhone with PyObjC”.

I replied “The presenter said some people seemed to get it working after jailbreaking their phones and installing python manually on it.” and asked for more information from Mikko. Here it is:

Here is some old discussion:

http://www.telesphoreo.org/pipermail/iphone-python/2008-November/000244.html (check also December)

  • Jailbreaking definitely works
  • PyObjC without ctypes should work even without jailbreaking
  • It is possible to write Python apps for Appstore - there is no legal issues as long you cripple Python a bit or sign your binaries
  • It might not be worth of the trouble to use Python on iPhone, since you have nice HTML5 + Javascript frameworks with direct native bridge like Phonegap: http://phonegap.com/ for which you can use to accomplish almost everything
Reflection
blog comments powered by Disqus
 
vanrees.org logo

About me

My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):