Using 'in' to match an attribute of Python objects in an array
<p>I don't remember whether I was dreaming or not but I seem to recall there being a function which allowed something like,</p>

<pre><code>foo in iter_attr(array of python objects, attribute name)</code></pre>

<p>I've looked over the docs but this kind of thing doesn't fall under any obvious listed headers</p>
<p>Using a list comprehension would build a temporary list, which could eat all your memory if the sequence being searched is large. Even if the sequence is not large, building the list means iterating over the whole of the sequence before <code>in</code> could start its search.</p>

<p>The temporary list can be avoiding by using a generator expression:</p>

<pre><code>foo = 12
foo in (obj.id for obj in bar)
</code></pre>

<p>Now, as long as <code>obj.id == 12</code> near the start of <code>bar</code>, the search will be fast, even if <code>bar</code> is infinitely long.</p>

<p>As @Matt suggested, it's a good idea to use <code>hasattr</code> if any of the objects in <code>bar</code> can be missing an <code>id</code> attribute:</p>

<pre><code>foo = 12
foo in (obj.id for obj in bar if hasattr(obj, 'id'))
</code></pre>
