[Biopython-dev] Bio._utils iterlen not needed

Peter Cock p.j.a.cock at googlemail.com
Thu Jan 10 21:16:12 UTC 2013


On Thu, Jan 10, 2013 at 5:24 PM, Michiel de Hoon <mjldehoon at yahoo.com> wrote:
> --- 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.

Good point - I didn't know xrange defined __len__, and you are
right in general - other iterator object could also do that:

https://github.com/biopython/biopython/commit/57ae89cdedbc1e18495ffb615a3a1d2c9feb0296

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

True. I hadn't checked that - I assumed it was used more
than once. If there are no other natural placed where it would
make sense then yes, it might as well be done in line once,
and Bio._utils.iterlen could be removed.

When written, iterlen was in private module Bio.Phylo._sugar
(CC'ing Eric) which Bow moved to Bio._utils as he wanted to
use some of it in SearchIO.

Peter




More information about the Biopython-dev mailing list