Can I iterate over a class in Python?
<p>I have a class that keeps track of its instances in a class variable, something like this:</p>

<pre><code>class Foo:
    by_id = {}

    def __init__(self, id):
        self.id = id
        self.by_id[id] = self
</code></pre>

<p>What I'd like to be able to do is iterate over the existing instances of the class.  I can do this with:</p>

<pre><code>for foo in Foo.by_id.values():
    foo.do_something()
</code></pre>

<p>but it would look neater like this:</p>

<pre><code>for foo in Foo:
    foo.do_something()
</code></pre>

<p>is this possible?  I tried defining a classmethod <code>__iter__</code>, but that didn't work.</p>
