Organizing Python Functions into Modules or Classes

code-organizationpython

I'm working on a module for clustering algorithms, clustering.py.

I noticed several functions in the module only pertain to the mean shift algorithm, so I thought it might make sense to group them under a MeanShift class.

Right now, it looks something like this:

class MeanShift:

    def cluster(args):
        *code*

    def update_mean_position(args):
        *code*

The update_mean_position function is called only by the cluster function, so I'm wondering if it should begin with an underscore to show it is for internal use only.

Also, is it acceptable to organize these functions in a class, or should they just remain as top-level functions in the clustering module?

Best Answer

Also, is it acceptable to organize these functions in a class, or should they just remain as top-level functions in the clustering module?

The usual advice is use a class when you have common data, and common functions, if your MeanShift class is entirely 'static' it could be left at toplevel, but if it has state it should probably be a class.

Any organization that helps code clarity is good, even if that means you have a 'static' class that is organizing 'static' methods!

Related Topic