thnx jonrsharpe,在阅读了您的评论、一些文档和文章、MyMeta 类之后:
from abc import ABCMeta
class MyMeta(ABCMeta):
def __init__(cls, name, bases, dct):
super(MyMeta, cls).__init__(name, bases, dct)
cls.allInstances = set()
父类
from MyMeta import MyMeta
class Basic(object):
__metaclass__ = MyMeta
def __init__(self):
self.allInstances.add(self)
def __del__(self):
self.allInstances.remove(self)
其他方式
from abc import ABCMeta, abstractmethod
class Basic(object):
__metaclass__ = ABCMeta
allInstances = None
def __init__(self):
if self.__class__.allInstances is None:
self.__class__.allInstances = set()
self.__class__.allInstances.add(self)
def __del__(self):
self.__class__.allInstances.remove(self)