[Biopython-dev] Bio._utils iterlen not needed

Michiel de Hoon mjldehoon at yahoo.com
Thu Jan 10 17:24:14 UTC 2013


--- On Thu, 1/10/13, Peter Cock <p.j.a.cock at googlemail.com> wrote:
> > Simply calling len(items) does exactly what iterlen
> does, and is much faster too.
> 
> No, the reason d'être for iterlen is that you can't use len
> on an iterator, e.g.
> 
> >>> len(iter("abcde"))
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: object of type 'iterator' has no len()
> 
You're right. Actually it depends on the iterator. For example,
len(xrange(100)) works (xrange also returns an iterator). I guess in general an iterator can't have a len() function because it's not clear that the iterator will ever end.

That said, currently the iterlen function is used in only one place, in Bio/Phylo/BaseTree.py as follows:

    def count_terminals(self):
        return _utils.iterlen(self.find_clades(terminal=True))

But here you could simply have

    def count_terminals(self):
        clades = self.find_clades(terminal=True)
        count = 0
        for clade in clades:
            count+=1
        return count

I don't see why we need a function iterlen for this, and if we do have such a function, why it should be in Bio._utils.

Best,
-Michiel.





More information about the Biopython-dev mailing list