python - How to create objects for a class automatically? -
is there way auto create objects in python
class a: pass = a()
i automatically create objects class. need parse bunch of xml , i'd create object 1 file (entry parse). or need create random names?
you don't have manually assign variable names object instances, store them in list when create them dynamically, in example list of objects gets created information file:
class a: def __init__(self, words): self.words = words a_objects = [] file = open("testfile.txt") line in file: words = line.split() a_objects.append(a(words))
if need access objects directly using key, have use dictionary instead of list:
a_objects = {}
you can add key-value pairs this:
words = line.split() key = words[0] a_objects[key] = a(words)
Comments
Post a Comment