我想为我创建的每个对象创建一个唯一的 ID - 这是该类:
class resource_cl :
def __init__(self, Name, Position, Type, Active):
self.Name = Name
self.Position = Position
self.Type = Type
self.Active = Active
我希望有一个 self.ID ,每次创建对该类的新引用时都会自动递增,例如:
resources = []
resources.append(resource_cl('Sam Sneed', 'Programmer', 'full time', True))
我知道我可以参考resource_cl,但我不知道如何从那里继续......
尝试 python 3 中得票最高的答案,你会遇到一个错误,因为.next()
已被删除。
相反,您可以执行以下操作:
import itertools
class BarFoo:
id_iter = itertools.count()
def __init__(self):
# Either:
self.id = next(BarFoo.id_iter)
# Or
self.id = next(self.id_iter)
...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)