New in Python 2.6: ORM made easy
From the What’s New in Python 2.6 page:
A new data type in the collections module: NamedTuple(typename, fieldnames) is a factory function that creates subclasses of the standard tuple whose fields are accessible by name as well as index. For example:
var_type = collections.NamedTuple(‘variable’, ‘id name type size’)
var = var_type(1, ‘frequency’, ‘int’, 4)
print var[0], var.id # Equivalent
print var[2], var.type # Equivalent
Neat! It all exists in libraryspace, so it’s not a groundbreaking change to the language. Still, this has to save time. Without this creating the same sort of class would have taken something like:
class variable(object):
def __init__(self, id, name, typ, size):
self.id, self.name, self.type, self.size = id, name, typ, size
Not too much typing, but certainly more, and you can’t even access it as a tuple without additional work. I wonder if the various Python ORM libraries use techniques like this?