Tuesday, August 12, 2008

Keeping Python objects backward compatible

In Python, its easy to maintain backward compatibility with your object designs. The most common problem is changing the interface of objects. This includes replacing old attribute names with old ones, etc.

With Python, the client which requires these interfaces, does not even need to change their code! This is easy if you just keep the old attributes. But this can lead to a maintenance nightmare. A better alternative might be to define a __getattr__() method that groups deprecated attributes. For instance,

class Blog:
def __init__(self):

def __getattr__(self, name):
return self._attrmap[name]

This gives us a _attrmap dictionary which we can then use to easily manage deprecated attributes.