Note that there are some explanatory texts on larger screens.

plurals
  1. POSame iterator object yields different result in for loop?
    primarykey
    data
    text
    <p>I came across a very strange behaviour in Python. Using a class derived from <code>UserDict</code>, the iterator <code>a.items()</code> behaves differently in a for loop than <code>a.data.items()</code>, even though the two are <strong>identical</strong>:</p> <pre><code>Python 3.3.1 (default, Apr 17 2013, 22:32:14) [GCC 4.7.3] on linux Type "help", "copyright", "credits" or "license" for more information. &gt;&gt;&gt; from datastruct import QueueDict &gt;&gt;&gt; a=QueueDict(maxsize=1700) &gt;&gt;&gt; for i in range(1000): ... a[str(i)]=1/(i+1) ... &gt;&gt;&gt; a.items() ItemsView(OrderedDict([('991', 0.0010080645161290322), ('992', 0.0010070493454179255), ('993', 0.001006036217303823), ('994', 0.0010050251256281408), ('995', 0.001004016064257028), ('996', 0.0010030090270812437), ('997', 0.001002004008016032), ('998', 0.001001001001001001), ('999', 0.001)])) &gt;&gt;&gt; a.data.items() ItemsView(OrderedDict([('991', 0.0010080645161290322), ('992', 0.0010070493454179255), ('993', 0.001006036217303823), ('994', 0.0010050251256281408), ('995', 0.001004016064257028), ('996', 0.0010030090270812437), ('997', 0.001002004008016032), ('998', 0.001001001001001001), ('999', 0.001)])) &gt;&gt;&gt; a.items()==a.data.items() True &gt;&gt;&gt; # nevertheless: ... &gt;&gt;&gt; for item in a.items(): print(item) ... ('992', 0.0010070493454179255) &gt;&gt;&gt; for item in a.data.items(): print(item) ... ('993', 0.001006036217303823) ('994', 0.0010050251256281408) ('995', 0.001004016064257028) ('996', 0.0010030090270812437) ('997', 0.001002004008016032) ('998', 0.001001001001001001) ('999', 0.001) ('991', 0.0010080645161290322) ('992', 0.0010070493454179255) &gt;&gt;&gt; </code></pre> <p>The class definition is as follows:</p> <pre><code>import collections, sys class QueueDict(collections.UserDict): def __init__(self, maxsize=1*((2**10)**2), *args, **kwargs ): self._maxsize=maxsize super().__init__(*args, **kwargs) self.data=collections.OrderedDict(self.data) def __getitem__(self, key): self.data.move_to_end(key) return super().__getitem__(key) def __setitem__(self, key, value): super().__setitem__(key, value) self._purge() def _purge(self): while sys.getsizeof(self.data) &gt; self._maxsize: self.data.popitem(last=False) </code></pre> <p>This is quite disturbing. Any ideas how the same object [by "visual" inspection, and also by <code>(a.items()==a.data.items()) == True</code>] can, and why it does, behave differently in the for loop?</p> <p>Thanks for your help and ideas!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload