1

I currently have a parent abstract class that has many children that inherit from it. Basic structure looks like this:

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.

To me it makes much more sense to split this code into files: parent_class.py child_class1.py child_class2.py child_class3.py etc etc

I could do this and just import the parent class into the child class but it doesn't make sense to have a parent module. The parent class is abstract and it doesn't make sense to have an instance of the parent. In addition I like the way things are currently inherited and importing the parent class seems like it will change this.

Essentially is it possible to split this code into files as above without making significant changes to the code? The only way I can figure out how to do this is to have the parent class in every file but this doesn't seem "right"....

ojunk
  • 879
  • 8
  • 21
  • You can split it whatever you want. Where did you catch an error? – vZ10 Jun 14 '17 at 10:23
  • This is definitely possible. https://stackoverflow.com/questions/4142151/how-to-import-the-class-within-the-same-directory-or-sub-directory – Aseem Bansal Jun 14 '17 at 10:25
  • "I could do this and just import the parent class into the child class but it doesn't make sense to have a parent module." Yes it does, just create a parent module and import it. – Markus Meskanen Jun 14 '17 at 10:43
  • Here's an example for you @ojunk, the `entity.py` contains the base `Entity` class, and `hero.py` and `skill.py` both contain subclasses: https://github.com/Mahi/Warcraft-SP/tree/master/srcds/addons/source-python/plugins/warcraft/entities – Markus Meskanen Jun 14 '17 at 10:49
  • Thanks all! It looks like I'd read everything that I needed but I hadn't quite put it all together properly. Markus Meskanen your link was very helpful thank you. Aseem Bansal your link was perfect for me fitting things together. If you want to make that an answer then I'm happy to accept that. – ojunk Jun 14 '17 at 13:40

1 Answers1

4

You can have parent class separate with child class and where ever you want. But only thing is that make sure that you import parent class first in your child module before use.

nanithehaddock
  • 247
  • 1
  • 9