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?

+2


source to share


6 answers


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.

+2


source


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.

+4


source


I would just add the underscored names _

to show that the reader shouldn't be concerned about them. This is conventionally the same as private members in other languages.

0


source


Place them in the Path class and note that they are "obscure" with either comments or docstrings. If you like, separate them at the end.

0


source


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.

0


source


I suggest making them available from a property of the Path class called "Utilties". For example: Path.Utilities.RazzleDazzle

. It will help with auto completion tools and general maintenance.

0


source







All Articles