gpt4 book ai didi

python - 如何在 Python 类中创建增量 ID

转载 作者:IT老高 更新时间:2023-10-28 20:29:29 26 4
gpt4 key购买 nike

我想为我创建的每个对象创建一个唯一 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,但我不确定如何从那里继续...

最佳答案

简洁优雅:

import itertools

class resource_cl():
newid = itertools.count().next
def __init__(self):
self.id = resource_cl.newid()
...

关于python - 如何在 Python 类中创建增量 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1045344/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com