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

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -