Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.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_List_Class_Object_Base Class - Fatal编程技术网

在基类模块中用python填充类列表

在基类模块中用python填充类列表,python,list,class,object,base-class,Python,List,Class,Object,Base Class,我希望在基类文件中有一个类列表,其中包含在运行时创建的派生类: BaseClass.py: # This is a list of objects to populate, holds classes (and not instances) MyClassesList=[] class MyBase(object): name='' #name of the specific derived class def __init__(self): pass 这是

我希望在基类文件中有一个类列表,其中包含在运行时创建的派生类:

BaseClass.py:

# This is a list of objects to populate, holds classes (and not instances)
MyClassesList=[]

class MyBase(object):
    name='' #name of the specific derived class

    def __init__(self):
        pass
这是一个真实的例子:

# This is a list of objects to populate, holds classes (and not instances)
MyClassesList=[]

class MyBase(object):
    name='' #name of the specific derived class

    def __init__(self):
        pass
我无法修改任何派生类代码,因此我希望在基类中维护添加的服务器列表,然后在运行时访问此列表

# Populate the Servers list automatically.
# This is a list of available servers to choose from, holds classes (and not instances)

Servers=[]

class ServerBase(object):
    name='' #name of the specific server class, for each server class

    def __init__(self):
        self.connected = False

    def __del__(self):
        self._disconnect()

    def connect(self):
        DBG("connect called for server {self.name}, is already connected: {self.connected}")
        if self.connected: return
        self._connect()
        self.connected = True


    def get_data(self):
        self.connected or self.connect()
        data=''
        # We're obligated to read the data in chunks.
        for i in range(100):
            data += self._get_data()
        return data

    def _connect(self):
        raise NotImplementedError("Interface Function Called")

    def _disconnect(self):
        raise NotImplementedError("Interface Function Called")

    def _get_data(self):
        raise NotImplementedError("Interface Function Called")

假设您想要的是在运行时创建的派生类对象列表(尽管不知道为什么需要)

当创建派生类的对象时,在基类的
\uuuu init\uuuu
函数中,传入的self将是派生类的对象,除非派生类重写
\uuu init\uuuu()
函数,并且不调用
super()。\uu init()
,在这种情况下,我不确定这是否可行

如果控制派生类,可以在派生类“
\uuuuu init\uuuu()
中调用
super()。\uuuu init\uuuu()
然后让
\uuuu init\uuuuu()
函数根据需要将该对象保存到列表中

您可以使用该
self
添加到您想要的列表中

下面的简单测试可能会对您有所帮助-

class CA:
    def __init__(self):
        print('Type - ' + str(type(self)))

>>> CA()
Type - <class '__main__.CA'>
<__main__.CA object at 0x006B9830>



class CASub(CA):
    pass

>>> CASub()
Type - <class '__main__.CASub'>
<__main__.CASub object at 0x006B9890>
CA类:
定义初始化(自):
打印('Type-'+str(Type(self)))
>>>CA()
类型-
B类(CA):
通过
>>>CASub()
类型-

可能有更好的方法,这是一种方法。

您能提供更多信息吗?这些派生类是如何在运行时创建的?这通常是由元类或工厂方法完成的。在本例中,我建议您看看元类是如何工作的。虽然我不得不说这听起来不是个好主意。