Best way to generate a consecutive id in Python
<p>Given a class that has an attribute <code>self.id</code> I need to populate that attribute with a counter starting at 0 and all objects of that class need a unique id during a single run of the program. What is the best/ most pythonic way of doing this? Currently I use</p>

<pre><code>def _get_id():
    id_ = 0
    while True:
       yield id_
       id_ += 1
get_id = _get_id()
</code></pre>

<p>which is defined outside the class and</p>

<pre><code>self.id = get_id.next()
</code></pre>

<p>in the class' <code>__init__()</code>. Is there a better way to do this? Can the generator be included in the class?</p>
