How to create multiple class objects with a loop in python?

后端 未结 5 1044
抹茶落季
抹茶落季 2020-12-04 15:48

Suppose you have to create 10 class objects in python, and do something with them, like:

obj_1 = MyClass()
other_object.add(obj_1)
obj_2 = MyClass()
other_ob         


        
5条回答
  •  -上瘾入骨i
    2020-12-04 16:20

    I hope this is what you are looking for.

    class Try:
        def do_somthing(self):
            print 'Hello'
    
    if __name__ == '__main__':
        obj_list = []
        for obj in range(10):
            obj = Try()
            obj_list.append(obj)
    
        obj_list[0].do_somthing()
    

    Output:

    Hello
    

提交回复
热议问题