User talk:WillNess: Difference between revisions
Content added Content deleted
No edit summary |
(citing docs) |
||
Line 2: | Line 2: | ||
Hi, you made an edit to [[Hamming numbers]] with the comment "according to docs for tee() storage is NOT shared among its iterators". I would like to know what you mean. What does it mean that storage is not shared. --[[Special:Contributions/208.80.119.67|208.80.119.67]] 00:49, 9 September 2011 (UTC) |
Hi, you made an edit to [[Hamming numbers]] with the comment "according to docs for tee() storage is NOT shared among its iterators". I would like to know what you mean. What does it mean that storage is not shared. --[[Special:Contributions/208.80.119.67|208.80.119.67]] 00:49, 9 September 2011 (UTC) |
||
:[http://docs.python.org/library/itertools.html#itertools.tee the docs] says it is "equivalent to": |
|||
<lang Python>def tee(iterable, n=2): |
|||
it = iter(iterable) |
|||
deques = [collections.deque() for i in range(n)] |
|||
def gen(mydeque): |
|||
while True: |
|||
if not mydeque: # when the local deque is empty |
|||
newval = next(it) # fetch a new value and |
|||
for d in deques: # load it to all the deques |
|||
d.append(newval) |
|||
yield mydeque.popleft() |
|||
return tuple(gen(d) for d in deques) |
|||
</lang> |
|||
:I read it to show that each iterator produced by <code>tee()</code> holds on to its own dequeue (list with pop() and append()). When a new item is needed from the underlying generator, it is appended separately into each one of the dequeues. Were they all to share one dequeue amongst themselves, that would be a shared storage. <code>popleft()</code> wouldn't be always called, but only when the ''leftmost'' of internal iterators pointing into internal list would get advanced. [[User:WillNess|WillNess]] 10:53, 9 September 2011 (UTC) |