Python增量对象名称
自从我学习了Python类的基础知识之后,我就一直在思考一个问题:如何自动创建对象,而不是每次都必须键入Python增量对象名称,python,Python,自从我学习了Python类的基础知识之后,我就一直在思考一个问题:如何自动创建对象,而不是每次都必须键入test1=class(示例,test)?我尝试在类中使用静态函数来创建函数: class test: num_of_t = 0 coomers = [] def __init__(self, tes, t): self.test = tes self.t = t te
test1=class(示例,test)
?我尝试在类中使用静态函数来创建函数:
class test:
num_of_t = 0
coomers = []
def __init__(self, tes, t):
self.test = tes
self.t = t
test.num_of_t += 1
@staticethod
def creat():
example_input = input("test:")
example_input2 = input("test2:")
test.coomers.append("test_{}".format(test.num_of_t))
for x in test.coomers:
x = test(example_input, example_input2)
def starter():
while y<10:
y += 1
test.creat()
类测试:
t的数量=0
库默斯=[]
定义初始值(自、tes、t):
自检=tes
self.t=t
test.num_of_t+=1
@静态法
def create():
示例输入=输入(“测试:”)
示例_input2=input(“test2:”)
append(“test_{}”.format(test.num_of_t))
对于test.coomers中的x:
x=测试(示例输入,示例输入2)
def starter():
而y最好使用字典保存类的创建实例,如下所示:
def starter():
key_list = [f'test_{i+1}' for i in range(10)]
instance_dict = {}
fixed_tes = 3
fixed_t = 7
for k in key_list:
instance_dict[k] = test(fixed_tes, fixed_t)
变量的名称只能通过在代码中写入来给出,没有其他方法,这不是对象的一部分,只是代码中的名称