Python: How to count the number of objects created?

前端 未结 5 1077
迷失自我
迷失自我 2020-12-20 22:46

I\'m new to Python. My question is, what is the best way to count the number of python objects for keeping track of number of objects exist at any given time? I thought of u

5条回答
  •  余生分开走
    2020-12-20 23:38

    class obj:
    count = 0
    def __init__(self,id,name):
        self.id = id
        self.name = name
        obj.count +=1
        print(self.id)
        print(self.name)
    
    o1 = obj(1,'vin')
    o2 = obj(2,'bini')
    o3 = obj(3,'lin')
    print('object called' ,obj.count)
    

提交回复
热议问题