Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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中完整对象和子对象的计数_Python_Class_Class Variables_Subobject - Fatal编程技术网

Python中完整对象和子对象的计数

Python中完整对象和子对象的计数,python,class,class-variables,subobject,Python,Class,Class Variables,Subobject,我想保持A和B对象的计数,B是A的子类。因此计数应该特定于A和B。例如,如果我创建了3个A对象和2个B对象,通过构造函数调用,A的计数变为3+2=5,但我想保持为3(不是作为B的一部分用作子对象时)。请对以下代码段发表评论: class A: acount = 0 # class variable def __init__(self, isFullA = True): if (isFullA): self.iamFullA = True

我想保持A和B对象的计数,B是A的子类。因此计数应该特定于A和B。例如,如果我创建了3个A对象和2个B对象,通过构造函数调用,A的计数变为3+2=5,但我想保持为3(不是作为B的一部分用作子对象时)。请对以下代码段发表评论:

class A:
    acount = 0 # class variable
    def __init__(self, isFullA = True):
        if (isFullA):
            self.iamFullA = True
            A.acount += 1
        else:
            self.iamFullA = False
    def __del__(self):
        if (self.iamFullA):
            A.acount -= 1

class B(A):
    bcount = 0 # class variable
    def __init__(self, isFullB = True):
        A.__init__(self,False)
        if (isFullB):
            self.iamFullB = True
            B.bcount += 1
        else:
            self.iamFullB = False
    def __del__(self):
        if (self.iamFullB):
            B.bcount -= 1
#MAIN
L=[]
for i in range(3):
  L.append(A())
for i in range(2):
  L.append(B())
print "A.acount = " + str(A.acount)
print "B.bcount = " + str(B.bcount)
输出为:

A.acount = 3
B.bcount = 2

您正在使其变得复杂-您只需为每个类设置一个独特的
count
class属性:

class A(object):
    _counter = 0

    @classmethod
    def _inc(cls):
        cls._counter += 1

    @classmethod
    def _dec(cls):
        cls._counter -= 1

    @classmethod
    def get_count(cls):
        return cls._counter

    def __init__(self):
        self._inc()

    def __del__(self):
        self._dec()


class B(A):
    _counter = 0

    def __init__(self, wot):
        super(B, self).__init__()
        self.wot = wot

L=[]
for i in range(3):
  L.append(A())
for i in range(2):
  L.append(B(i))
print "A.count = {}".format(A.get_count())
print "B.count = {}".format(B.get_count())
请注意,我使用了
classmethods
来确保我们正在访问class属性,因为
self.\u counter+=1
中的
\uuu init\uuu
将创建一个实例属性。您还可以使用
type(self)。\u counter+=1
(或
self.\uu class.\uu counter+=1
)获得正确的行为,但这有点难看

如果这是其他开发人员将构建的API,您可能希望使用自定义元类来确保每个子类都有自己的
\u计数器,即:

 class CounterType(type):
    def __new__(meta, name, bases, attribs):
        if "_counter" not in attribs:
            attribs["_counter"] = 0
        return type.__new__(meta, name, bases, attribs)

class CounterBase(object):
    __metaclass__ = CounterType
    @classmethod
    def _inc(cls):
        cls._counter += 1

    @classmethod
    def _dec(cls):
        cls._counter -= 1

    @classmethod
    def get_count(cls):
        return cls._counter

    def __init__(self):
        self._inc()

    def __del__(self):
        self._dec()


class A(CounterBase):
    pass


class B(A):
    def __init__(self, wot):
        super(B, self).__init__()
        self.wot = wot

L=[]
for i in range(3):
  L.append(A())
for i in range(2):
  L.append(B(i))
print "A.count = {}".format(A.get_count())
print "B.count = {}".format(B.get_count())

那么,你的问题是什么?如果您只想查看代码,请转到。如果isinstance(self,A):A.acount+=1
,则如何处理