[英]Create an incremental ID in a Python class with subclasses each maintaining their own ID series
我想创建一个 class,其中 class 的每个 object 都被赋予一个从0
开始的唯一顺序 ID。 我还希望每个子类都具有从0
开始的顺序 ID,编号不受父类或兄弟类的影响。
使用类似问题的答案,我创建了 class Foo
class Foo:
id_iter = itertools.count()
def __init__(self):
self.id = next(self.id_iter)
Foo
的所有实例都将具有顺序id
值。 我还想创建一个 class Bar
继承自Foo
这样
>>> Foo().id
0
>>> Foo().id
1
>>> Foo().id
2
>>> Bar().id # The first Bar id starts at 0 rather than 3
0
如果我像这样创建Bar
:
class Bar(Foo):
def __init__(self):
super(Bar, self).__init__()
上面对Bar().id
的调用将返回3
,因为两个类都使用相同的迭代器。 或者,我可以将id_iter = itertools.count()
添加回我的Bar
定义并获得我期望的 output。 但是,我正在制作Foo
的多个子类,并且不想将该行添加到每个 class; 我希望Foo
的所有子类都自动继承 ID 功能,而不必向子类定义添加任何额外内容。 我怎样才能做到这一点?
您不能使用 class 属性执行此操作,因为 class 属性不会被继承。 一种方法可能是使用单独的字典来保持类的计数。 例如:
import itertools
class ClassCounter(object):
counters = {}
@classmethod
def get_counter(cls, class_to_count):
cls.counters.setdefault(class_to_count, itertools.count())
return next(cls.counters[class_to_count])
class Foo:
def __init__(self):
self.id = ClassCounter.get_counter(self.__class__)
class Bar(Foo):
def __init__(self):
super(Bar, self).__init__()
if __name__ == '__main__':
foo1 = Foo()
foo2 = Foo()
foo3 = Foo()
bar1 = Bar()
bar2 = Bar()
print(f"{foo1.id=}, {foo2.id=}, {foo3.id=}, {bar1.id=}, {bar2.id=}")
这应该打印:
foo1.id=0, foo2.id=1, foo3.id=2, bar1.id=0, bar2.id=1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.