As you said in the question, _id is shared by the parent and all child classes. Define _id for each child class.
from itertools import count class Parent(object): base_id = 0 _id = count(0) def __init__(self): self.id = self.base_id + self._id.next() class Child1(Parent): base_id = 100 _id = count(0)
UPDATE
Using the metaclass:
class IdGenerator(type): def __new__(mcs, name, bases, attrs): attrs['_id'] = count(0) return type.__new__(mcs, name, bases, attrs) class Parent(object): __metaclass__ = IdGenerator base_id = 0 def __init__(self): self.id = self.base_id + next(self._id)
falsetru
source share