【问题标题】:Python: How to count the number of objects created?Python:如何计算创建的对象数量?
【发布时间】:2011-05-30 17:32:51
【问题描述】:

我是 Python 新手。我的问题是,计算python对象数量以跟踪任何给定时间存在的对象数量的最佳方法是什么?我想到了使用静态变量。

我已经阅读了几篇关于 Python 静态变量的问答,但我不知道如何使用静态来实现对象计数。

我的尝试是这样的(如下),从我的 C++ 背景来看,我希望它能够工作,但它没有。我不是iMenuNumber 一个静态成员,它应该在每次创建对象时递增?

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

    iMenuNumber = 0

    def __init__ (self, iSize):
        self.iMenuNumber = self.iMenuNumber + 1
        self.iMenuSize = iSize

def main():
   objAutoTester = baseMENUS(MENU_SIZE_1)
   ....
   ....
   ....
   objRunATest = baseMENUS(MENU_SIZE_2)

我还没有写 delete(del) 函数(析构函数)。

【问题讨论】:

    标签: python object static count


    【解决方案1】:

    使用self.__class__.iMenuNumberbaseMENUS.iMenuNumber 而不是self.iMenuNumber 在类而不是实例上设置var。

    此外,Hungarian Notation 不是 Pythonic(实际上,它在所有语言中都很糟糕)——您可能想停止使用它。请参阅http://www.python.org/dev/peps/pep-0008/ 获取一些代码样式建议。

    【讨论】:

    • "Systems hungarian" 很烂,因为它告诉您没有任何尚未在类型声明中编码的内容(静态类型),或者根本什么都没有,因为每个变量都是某种不受限制的变体(动态类型)。 Apps 匈牙利语并没有那么糟糕,甚至可以说是一个非常好的主意。
    • 另请注意,python 具有“就地”运算符,这将使所讨论的行更短并且(恕我直言)更具可读性:baseMENUS.iMenuNumber += 1
    【解决方案2】:

    我认为你应该使用baseMENUS.iMenuNumber 而不是self.iMenuNumber

    【讨论】:

      【解决方案3】:

      请注意,以上两个答案都是正确的,但它们有很大的不同。不仅在您编写它们的方式上,而且在最终结果中。

      如果您从 baseMENUS 类派生,就会出现差异。

      在 n.m. 的解决方案中,计数器对于从 baseMENUS 派生的任何类的所有实例都是相同的。另一方面,在 ThiefMaster 的情况下;每个从 baseMENUS 派生的不同类都会有计数器。

      在下面的示例中。我从 baseMENUS 派生了两个类。它们是 AMENUS 和 BMENUS;我创建了 3 个 AMENUS 实例和 4 个 BMENUS 实例。

      当我使用n.m的方法时,计数器一直到7。

      当我使用 ThiefMaster's 时,我创建了 2 个指示物。一个到 3,另一个到 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]
      

      创建的输出是:

      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
      

      很抱歉迟到了 5 年才加入讨论。但我觉得这增加了它。

      【讨论】:

        【解决方案4】:
        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)
        

        【讨论】:

          【解决方案5】:

          我会执行以下操作

          类baseMENUS: """用于显示菜单的类"""

          iMenuNumber = 0
          
          def __init__ (self, iSize):
              baseMENUS.iMenusNumber += 1
              self.iMenuSize = iSize
          
          def main():
             objAutoTester = baseMENUS(MENU_SIZE_1)
             ....
             ....
             ....
             objRunATest = baseMENUS(MENU_SIZE_2)
          

          【讨论】:

            猜你喜欢
            • 2020-03-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-07-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-04-14
            相关资源
            最近更新 更多