在Python中,子类可以与父类在不同的文件中吗?

2024-04-19 06:30:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前有一个父抽象类,它有许多继承自它的子类。基本结构如下:

import sqlite3
import os
from abc import ABCMeta, abstractmethod

class ParentClass(object):
        """DOCSTRING"""
        __metaclass__ = ABCMeta

        def __init__(self, base_path, state_name):
        ... other methods etc

class ChildClass1(ParentClass):

        def __init__(self, base_path):
        """DOCSTRING"""
            state_name = 'chromosome'
            super().__init__(base_path, state_name)

        ...other methods etc

class ChildClass2(ParentClass):

        def __init__(self, base_path):
        """DOCSTRING"""
            state_name = 'ftszRing'
            super().__init__(base_path, state_name)

       ...other methods etc

...other child classes that all inherit from the abstract parent class.

对我来说,将这些代码拆分为文件更有意义: 家长_类.py 儿童1级.py child_类2.py child_类3.py 等等

我可以这样做,只需将父类导入到子类中,但是拥有父模块是没有意义的。父类是抽象的,拥有父类的实例没有意义。另外,我喜欢事物当前被继承的方式,导入父类似乎会改变这一点。在

本质上,是否可以像上面那样将代码拆分为文件,而不必对代码进行重大更改?我唯一能弄明白如何做到这一点的方法是在每个文件中都有父类,但这似乎并不“正确”。。。。在


Tags: pathnamepyimportselfbaseinitdef