Python Meta Programming: Update

November 29th, 2007

I thought I was being really clever with the meta programming from the previous post but I ran into some roadblocks when using similar techniques in live code. I pretty much scrapped the meta programming stuff for now until I can do some more research and develop something solid. At least the problems showed themselves early on before I really became attached to the code. Here’s to failing early and often! ;)

Python Meta Programming

November 27th, 2007

For an ORM (Object Relational Mapper) I’m working on, I was trying to figure out how I can make it connect to a database without manually calling any functions. Using MySQLdb, you connect to a database by calling MySQLdb.connect(). I wanted this to happen automatically so I’m not always calling MySQLdb.connect() or the equivalent from my own ORM.

The technique I used was to initialize the class when it’s first defined. In that __classinit__ method I set up the database connection. The metaclass I used is pretty straightforward:

The class above is used as the __metaclass__ of another class which has a __classinit__ function that sets up the database connection.

So now you can create your model classes that inherit from MySQLRecord and it will create a database connection if one doesn’t already exist.

I highly encourage anyone interested in python metaprogramming to rip apart these techniques and others to get a better feel of what’s going on under the hood. A good place to start would be to set up your own metaclasses and print out the various variables and follow what happens in what order. I’ll be doing follow up posts on python metaprogramming that will clear up any fuzzy details.