How to override Python list(iterator) behaviour?
<p>Running this:</p>

<pre><code>class DontList(object):
    def __getitem__(self, key):
        print 'Getting item %s' % key
        if key == 10: raise KeyError("You get the idea.")
        return None

    def __getattr__(self, name):
        print 'Getting attr %s' % name
        return None

list(DontList())
</code></pre>

<p>Produces this:</p>

<pre><code>Getting attr __length_hint__
Getting item 0
Getting item 1
Getting item 2
Getting item 3
Getting item 4
Getting item 5
Getting item 6
Getting item 7
Getting item 8
Getting item 9
Getting item 10
Traceback (most recent call last):
  File "list.py", line 11, in &lt;module&gt;
    list(DontList())
  File "list.py", line 4, in __getitem__
    if key == 10: raise KeyError("You get the idea.")
KeyError: 'You get the idea.'
</code></pre>

<p>How can I change that so that I'll get <code>[]</code>, while still allowing access to those keys <code>[1]</code> etc.?</p>

<p>(I've tried putting in <code>def __length_hint__(self): return 0</code>, but it doesn't help.)</p>

<p><strong>My real use case:</strong> (for perusal if it'll be useful; feel free to ignore past this point)</p>

<p>After applying <a href="http://code.google.com/p/iniparse/issues/detail?id=25" rel="nofollow">a certain patch</a> to iniparse, I've found a nasty side-effect to my patch. Having <code>__getattr__</code> set on my <code>Undefined</code> class, which returns a new <code>Undefined</code> object. Unfortunately, this means that <code>list(iniconfig.invalid_section)</code> (where <code>isinstance(iniconfig, iniparse.INIConfig)</code>) is doing this (put in simple <code>print</code>s in the <code>__getattr__</code> and <code>__getitem__</code>):</p>

<pre><code>Getting attr __length_hint__
Getting item 0
Getting item 1
Getting item 2
Getting item 3
Getting item 4
</code></pre>

<p>Et cetera ad infinitum.</p>
