I'm building a project and I have run into the following problem:
I have implemented several subclasses, each of them having about 250 lines of code. Semantically, they should go together in the same module and I want to import them with
from mymodule import SubclassA, SubclassB
But then my module file has thousands of lines, which makes maintaining its code pretty nasty. Now I have each class in a separate file to make it easier to maintain but I have to use it like this:
from subclassa import SubclassA
from subclassb import SubclassB
this does not make any sense and it's really awful.
Is there any elegant solution? If not, which of the aforementioned is the better solution?
You can always put the from subclassa ...
imports into your package's __init__.py
as you showed in your second listing. Then, they will be available directly off of your package as you wrote in your first listing.
I prefer the second solution, with separate files. The imports are not that awful.
If they bother you so much, you could encapsulate them in yet another file, and then import all classes secondhand from that file. So your main import would look like the first solution, but the sole contents of the mymodule file would be the code in the second solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With