[BioPython] named tuples for biopython?

Peter biopython at maubp.freeserve.co.uk
Fri Oct 17 10:17:51 UTC 2008


On Fri, Oct 17, 2008 at 11:03 AM, Giovanni Marco Dall'Olio
<dalloliogm at gmail.com> wrote:
> Hi,
> python 2.6 is going to implement a new kind of data (like lists, strings,
> etc..) called 'named_tuple'.  It is intended to be a better data format to
> be used when parsing record files and databases.

I'd just seen this today actually via another mailing list.  Here is a
short example which actually works on python 2.6 (the details have
changed slightly from your quote),

>>> from collections import namedtuple
>>> Person = namedtuple("Person", "name surname")
>>> x = Person("Albert", "Einstein")
>>> x
Person(name='Albert', surname='Einstein')
>>> x.name
'Albert'
>>> x.surname
'Einstein'
>>> x.keys()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Person' object has no attribute 'keys'
>>> x["name"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: tuple indices must be integers, not str
>>> x[0]
'Albert'
>>> x[1]
'Einstein'

So this doesn't act much like a dictionary (in terms of the x[...]
usage), so we can't use it as a drop in enhancement for existing
dictionaries in Biopython.  I expect there are some places where a
namedtuple would make sense (although using it might break backwards
compatibility).

Also, if we did want to use NamedTuple in Biopython we'd have to
include a copy for use on older versions of python.  This is probably
possible under the python license... but would require an
implementation that still worked on pre 2.6.

Peter



More information about the Biopython mailing list