Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用子类正确计数实例_Python 3.x_Oop_Inheritance_Instance_Counting - Fatal编程技术网

Python 3.x 使用子类正确计数实例

Python 3.x 使用子类正确计数实例,python-3.x,oop,inheritance,instance,counting,Python 3.x,Oop,Inheritance,Instance,Counting,我正在用OOP进行实验,并试图用每个类和子类的实例数实现一些类继承 我希望在class'\uuu init\uuu方法中增加一个class变量,这样当我创建一个类的实例时,该类及其所有父类和祖辈类的计数都会增加 然而,在多次尝试之后,我正在努力实现这一点,同时将每个子类的计数与父类隔离开来 假设我有“Cls”、“SubCls”和“SubSubCls”类,如下所示 class Cls(): count = 0 def __init__(self): #genera

我正在用OOP进行实验,并试图用每个类和子类的实例数实现一些类继承

我希望在class'
\uuu init\uuu
方法中增加一个class变量,这样当我创建一个类的实例时,该类及其所有父类和祖辈类的计数都会增加

然而,在多次尝试之后,我正在努力实现这一点,同时将每个子类的计数与父类隔离开来

假设我有“Cls”、“SubCls”和“SubSubCls”类,如下所示

class Cls():
    count = 0

    def __init__(self):
        #general init stuff
        self.__class__.count += 1

class SubCls(Cls):
    pass

class SubSubCls(SubCls):
    pass
我喜欢以下行为:

a = Cls()
SubSubCls.count #returns 0
SubCls.count    #returns 0
Cls.count       #returns 1

b = SubCls()
SubSubCls.count #returns 0
SubCls.count    #returns 1
Cls.count       #returns 2

c = SubSubCls()
SubSubCls.count #returns 1
SubCls.count    #returns 2
Cls.count       #returns 3
这样做的理由是,如果Cls类似于“实体”,SubCls是“物理实体”,SubSubCls是“树”,我创建了一个树的实例,我现在有一个树,一个物理实体,一个实体-即使它们都在一个对象中

到目前为止,我提出的最好的建议如下:

class Cls():
    count = 0

    def __init__(self):
        #general init stuff
        self.inc_count()

    def inc_count(self):
        Cls.count += 1
        super().inc_count() #removing this avoids AttributeError but means having
                            # unique methods for inc_count in all SubCls level Classes
class SubCls(Cls):
    count = 0

    def __init__(self):
        super().__init__()
        #unique init stuff

    def inc_count(self):
        SubCls.count += 1
        super().inc_count()        


class SubSubCls(SubCls):
    count = 0

    def __init__(self):
        super().__init__()
        #unique init stuff

    def inc_count(self):
        SubSubCls.count += 1
        super().inc_count()
这是正确的行为,但我觉得继承应该能够更好。我不想为每个类分别定义
inc\u count()
,因为它们非常相似-我想在这里保持事物的可伸缩性

  • 我可以看到多重继承的情况在将来会成为一种并发症,但我现在就不谈了

  • 我确信我可以绕过
    Cls
    定义中的
    super().inc\u count()
    ,使用try和except或通过在
    Cls
    上面定义
    MetaCls
    来造成属性错误,所以我不太担心

  • 但是,我很确定我不能简单地用一个
    self.\u class.\u.count+=1
    类型(self)替换
    inc.\u count()
    定义中的
    Cls.count+=1
    SubCls.count+=1
    ,以及
    SubSubCls.count+=1
    .count+=1
    ,因为这只会导致多次对实例的“最低”类的计数执行操作。(即,创建SubSubCls实例将导致SubSubCls的总数增加3,并且其父类的计数保持不变)

  • 为了解决这个问题,是否有一种抽象的方法来引用方法继承自的类?因此,我的inc_计数仅在顶层定义为类似的内容(并由所有子类继承)

    显然,如果我打算完全以错误的方式获得想要的效果,我会很乐意接受任何建议。我以前唯一一次尝试OOP是在12岁的时候使用Flash——很久以前了

    def inc_count(self):
        DirectParentClassDefinedAtRunTime.count += 1
        super().inc_count()