Converting a flat list to a list of tuples

Bengt Richter bokr at oz.net
Thu Nov 24 04:26:46 EST 2005


On Wed, 23 Nov 2005 13:23:21 +0100, "Fredrik Lundh" <fredrik at pythonware.com> wrote:

>Bengt Richter wrote:
>
>> Are you thinking of something like lines from a file, where there might be
>> chunky buffering? ISTM that wouldn't matter if the same next method was called.
>> Here we have multiple references to the same iterator. Isn't e.g. buiding
>> a plain tuple defined with evaluation one element at a time left to right?
>
>yeah, but what says that the iterator has to be called during tuple construction?
>
>    while 1:
>        for each sequence:
>            # optimize cache behaviour!
>            grab up to N items from each iterator
>        M = length of shortest output list
>        for i in range(M):
>            build tuple and append
>        if M != N:
>            break
>
I'd say it's ok to cache some iterator output, but the cache buffer
must be associated with the iterator like cachedict[id(it)]. Then
the loop would only pump up the same cache buffer, since all iterators
would refer to the same buffer, and the loop that builds the tuple would
draw one from "each" buffer unknowingly drawing from the same buffer,
since they would be all found by cachedict[id(it)] which would be the same.
This would preserve the identity and logical sequentiality of the data
stream(s). To buffer separately on the unverified assumption that they
are different iterators seems like a buggy implementation to me ;-)
(optimizing use of total available buffering space is another matter,
not to mention handling StopIteration in the buffer loading)

ISTM to me the easiest just to define the intuitive behaviour, and let
implementers buffer accordingly if they want to.

Regards,
Bengt Richter



More information about the Python-list mailing list