Code organization in Python: where is a good place to create obscure methods?
I have a class called Path
that has about 10 methods defined in a dedicated module Path.py
. I recently had to write 5 more methods for Path
, however, these new methods are rather obscure and technical, and in 90% of cases they don't matter.
Where would it be nice to place them so that their context is clear? Of course I can just put them with a class definition, but I don't like that because I like to separate important things from unclear things.
I currently have these methods as functions that are defined in a separate module, just to keep things separate, but it would be better to have them as related methods. (They currently accept an instance Path
as an explicit parameter.)
Does anyone have a suggestion?
source to share
If you are interested in putting these methods in a different source file at any cost, and try to use them with any methods, you can achieve both goals by using a different source file to define a mixin class and have your Path class import that method and multiply inherit from this mixin. Thus, it is technically feasible.
However, I would not recommend this course of action: it should use "big guns" (eg multiple inheritance) only to serve important purposes (eg reuse and repetition removal), and thus separate methods from a not very important purpose.
If these "obscure methods" didn't play any role, you wouldn't implement them, so in the end they should have SOME meanings; so I'll just clarify in the doxers and comments what they are for, maybe to directly mention that they are rarely needed and leave that to that.
source to share
If a method is relative to a Path - no matter how obscure it is - I think it should be inside the class itself.
If you have multiple locations where you have course-related functions, this leads to problems. For example, if you want to check if some functionality exists, how does a new programmer know to check other, less obvious places?
I think it might be good practice to order functions by importance. As you may have heard, some suggest putting public members of classes first and then private / protected. You might consider applying generic methods in your class above obscure ones.
source to share
Oh wait, I was thinking about something - I can just define them in a module Path.py
where each obscure method will be a one-liner that will call a function from a separate module that currently exists. With this tradeoff, obscure methods will probably have 10 lines at the end of the file, rather than 50% of the file size.
source to share