On 07Oct2011 11:43, Jean-Michel Pichavant <jeanmic...@sequans.com> wrote: | namedtuple sounds great (if you don't use unpacking :o) ), too bad | it is available only from python 2.6.
It is easy enough to roll your own. Here's some example code with several flaws (it claims tuplehood, but is actually a list; it is not immutable; it takes a list of field names instead of a space separated string as namedtuple does) and isn't very tested. But feel free to take it and adapt it: def NamedTupleClassFactory(*fields): ''' Construct classes for named tuples a bit like the named tuples coming in Python 2.6/3.0. NamedTupleClassFactory('a','b','c') returns a subclass of "list" whose instances have properties .a, .b and .c as references to elements 0, 1 and 2 respectively. ''' class NamedTuple(list): for i in range(len(fields)): f=fields[i] exec('def getx(self): return self[%d]' % i) exec('def setx(self,value): self[%d]=value' % i) exec('%s=property(getx,setx)' % f) return NamedTuple def NamedTuple(fields,iter=()): ''' Return a named tuple with the specified fields. Useful for one-off tuples/lists. ''' return NamedTupleClassFactory(*fields)(iter) More old code:-( I can see I need to go back to that and make it cleaner. Surely I can get rid of the exec(0s at least:-) Cheers, -- Cameron Simpson <c...@zip.com.au> DoD#743 http://www.cskk.ezoshosting.com/cs/ Hacker: One who accidentally destroys. Wizard: One who recovers afterwards. -- http://mail.python.org/mailman/listinfo/python-list