如何在Python中使用列表理解为类设置唯一ID?
我试图在我的列表理解中将每个类ID设置为x变量,其中ID为0,1,2…99 这在python中可能吗 主要功能:如何在Python中使用列表理解为类设置唯一ID?,python,Python,我试图在我的列表理解中将每个类ID设置为x变量,其中ID为0,1,2…99 这在python中可能吗 主要功能: def main(): sample_size = int(input("Enter a sample size to test")) people = [Person() for x in range(sample_size) Person().id = x] #<<<<<< Here people[0].infected
def main():
sample_size = int(input("Enter a sample size to test"))
people = [Person() for x in range(sample_size) Person().id = x] #<<<<<< Here
people[0].infected = True #Infect the first person
while True:
input("")
for i in range(sample_size):
person[i].adv()
def main():
样本大小=int(输入(“输入要测试的样本大小”))
people=[Person()表示范围内的x(示例大小)Person().id=x]#最简单的方法是将id作为构造函数参数传递:
班级人员:
定义初始化(self,id):
self.id=id
self.infected=False
self.symptoms=False
self.recovered=False
self.dead=False
自感染以来的self.days\u=0
#其他方法。。。
然后在列表中使用它:
people = [Person(x) for x in range(sample_size)]
# Here ----------^
哇!谢谢你,我不敢相信我忽略了这一点。对班级来说是相当新的。再次感谢!是的,是的,请澄清到底是什么问题。看见
people = [Person(x) for x in range(sample_size)]
# Here ----------^