Python: How to count the number of objects created?

前端 未结 5 1059
迷失自我
迷失自我 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:25

    I think you should use baseMENUS.iMenuNumber instead of self.iMenuNumber.

    0 讨论(0)
  • 2020-12-20 23:25

    Notice that both answers above are right, but they are very different. Not only in the way you write them but in the final result.

    The difference would come up if you were ever to derive from the baseMENUS class.

    In n.m.'s solution, the counter will be the same for ALL instantiations of any class derived from baseMENUS. In the case of ThiefMaster on the other hand; there will be counter for each different class derived from baseMENUS.

    In the example below. I derive two classes from baseMENUS. They are AMENUS and BMENUS; I create 3 instances of AMENUS and 4 instances of BMENUS.

    When I use n.m's method, The counter goes all the way up to 7.

    When I use ThiefMaster's I create 2 counters. One goes to 3 and the other one to 4:

    class baseMENUS:
        """A class used to display a Menu"""
        iMenuNumber = 0
        jMenuNumber = 0
        def __init__ (self):
            baseMENUS.iMenuNumber = baseMENUS.iMenuNumber + 1
            self.__class__.jMenuNumber = self.__class__.jMenuNumber + 1
            self.counterNAMEOFCLASS = baseMENUS.iMenuNumber
            self.counterclass = self.__class__.jMenuNumber
    
    class AMENUS(baseMENUS):
        def __init__(self, ):
            super(AMENUS, self).__init__()
    
    class BMENUS(baseMENUS):
        def __init__(self, ):
            super(BMENUS, self).__init__()
    
    allmenus = [AMENUS() for i in range(0,3)] + [BMENUS() for i in range(0,4)]
    [print('Counting using n.m. method:', i.counterNAMEOFCLASS, '. And counting using ThiefMaster method :', i.counterclass) for i in allmenus]
    

    The output created is:

    Counting using n.m. method: 1 . And counting using ThiefMaster method : 1
    Counting using n.m. method: 2 . And counting using ThiefMaster method : 2
    Counting using n.m. method: 3 . And counting using ThiefMaster method : 3
    Counting using n.m. method: 4 . And counting using ThiefMaster method : 1
    Counting using n.m. method: 5 . And counting using ThiefMaster method : 2
    Counting using n.m. method: 6 . And counting using ThiefMaster method : 3
    Counting using n.m. method: 7 . And counting using ThiefMaster method : 4
    

    I'm sorry to jump in 5 years late into the discussion. But I felt like this added to it.

    0 讨论(0)
  • 2020-12-20 23:27

    Use self.__class__.iMenuNumber or baseMENUS.iMenuNumber instead of self.iMenuNumber to set the var on the class instead of the instance.

    Additionally, Hungarian Notation is not pythonic (actually, it sucks in all languages) - you might want to stop using it. See http://www.python.org/dev/peps/pep-0008/ for some code style suggestions.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-12-20 23:46

    I would implement the following

    class baseMENUS: """A class used to display a Menu"""

    iMenuNumber = 0
    
    def __init__ (self, iSize):
        baseMENUS.iMenusNumber += 1
        self.iMenuSize = iSize
    
    def main():
       objAutoTester = baseMENUS(MENU_SIZE_1)
       ....
       ....
       ....
       objRunATest = baseMENUS(MENU_SIZE_2)
    
    0 讨论(0)
提交回复
热议问题