Project structure for class inheritance in Python

2.3k Views Asked by At

What is the best practice to deal with inheritance in Python in terms of organizing the project structure?

I am using something like this:

utils/Parser.py
utils/XMLParser.py
utils/JsonParser.py

Parser.py

class Parser():
  def parse(self):
    raise NotImplementedError("Implement me")

I stand for code readability as much as possible. Is there any best practice to improve readability on this by separating the abstract class from its implementations, let's say, by placing utils/Parser.py under utils/generic/Parser.py.

I am also open for better examples or better approaches.

1

There are 1 best solutions below

0
sam On

There are plenty of resources on Python module creation and organization. This guide includes lots of general-purpose advice and best practices.

As a very general rule, look no further than The Zen of Python (which you can also see by typing import this into the Python interpreter):

Flat is better than nested.

So for your specific case, it seems that your initial directory structure is fine. It's clear enough, and your imports will explain the rest. Of course, if you have many generic animals and need to separate them logically, you can do that too.