count items in generator

Paul Rubin http
Mon May 15 01:22:27 CEST 2006


"Delaney, Timothy (Tim)" <tdelaney at avaya.com> writes:
> > Actually len(itertools.count()) would as well - when a couple of long
> > instances used up everything available - but it would take a *lot*
> > longer.
> 
> Actually, this would depend on whether len(iterable) used a C integral
> variable to accumulate the length (which would roll over and never end)
> or a Python long (which would eventually use up all memory).

That's only because itertools.count itself uses a C int instead of a long.
IMO, that's a bug (maybe fixed in 2.5):

    Python 2.3.4 (#1, Feb  2 2005, 12:11:53) 
    [GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys,itertools
    >>> a=sys.maxint - 3
    >>> a
    2147483644
    >>> b = itertools.count(a)
    >>> [b.next() for i in range(8)]
    [2147483644, 2147483645, 2147483646, 2147483647, -2147483648,
    -2147483647, -2147483646, -2147483645]
    >>> 



More information about the Python-list mailing list