Why does Python not support record type? (i.e. mutable namedtuple) Why does Python not support record type? (i.e. mutable namedtuple) python python

Why does Python not support record type? (i.e. mutable namedtuple)


Python <3.3

You mean something like this?

class Record(object):    __slots__= "attribute1", "attribute2", "attribute3",    def items(self):        "dict style items"        return [            (field_name, getattr(self, field_name))            for field_name in self.__slots__]    def __iter__(self):        "iterate over fields tuple/list style"        for field_name in self.__slots__:            yield getattr(self, field_name)    def __getitem__(self, index):        "tuple/list style getitem"        return getattr(self, self.__slots__[index])>>> r= Record()>>> r.attribute1= "hello">>> r.attribute2= "there">>> r.attribute3= 3.14>>> print r.items()[('attribute1', 'hello'), ('attribute2', 'there'), ('attribute3', 3.1400000000000001)]>>> print tuple(r)('hello', 'there', 3.1400000000000001)

Note that the methods provided are just a sample of possible methods.

Python ≥3.3 update

You can use types.SimpleNamespace:

>>> import types>>> r= types.SimpleNamespace()>>> r.attribute1= "hello">>> r.attribute2= "there">>> r.attribute3= 3.14

dir(r) would provide you with the attribute names (filtering out all .startswith("__"), of course).


Is there any reason you can't use a regular dictionary? It seems like the attributes don't have a specific ordering in your particular situation.

Alternatively, you could also use a class instance (which has nice attribute access syntax). You could use __slots__ if you wish to avoid having a __dict__ created for each instance.

I've also just found a recipe for "records", which are described as mutable named-tuples. They are implemented using classes.

Update:

Since you say order is important for your scenario (and you want to iterate through all the attributes) an OrderedDict seems to be the way to go. This is part of the standard collections module as of Python 2.7; there are other implementations floating around the internet for Python < 2.7.

To add attribute-style access, you can subclass it like so:

from collections import OrderedDictclass MutableNamedTuple(OrderedDict):    def __init__(self, *args, **kwargs):        super(MutableNamedTuple, self).__init__(*args, **kwargs)        self._initialized = True    def __getattr__(self, name):        try:            return self[name]        except KeyError:            raise AttributeError(name)    def __setattr__(self, name, value):        if hasattr(self, '_initialized'):            super(MutableNamedTuple, self).__setitem__(name, value)        else:            super(MutableNamedTuple, self).__setattr__(name, value)

Then you can do:

>>> t = MutableNamedTuple()>>> t.foo = u'Crazy camels!'>>> t.bar = u'Yay, attribute access'>>> t.foou'Crazy camels!'>>> t.values()[u'Crazy camels!', u'Yay, attribute access']


This can be done using an empty class and instances of it, like this:

>>> class a(): pass... >>> ainstance = a()>>> ainstance.b = 'We want Moshiach Now'>>> ainstance.b'We want Moshiach Now'>>>