I created some data objects for my data in Python. Now, I need to write some algorithms that process this data. I am debating between writing the algorithms as:
(1) Methods of the Data objects. (2) Functions in a separate module. (3) Algorithm objects.
I noticed that in numpy, some algorithms (ex. min, max, sum etc) are implemented as methods of the numpy array object, while other more complex algorithms (ex. svd ) are implemented as functions (in a separate module called numpy.linalg). I also noticed that some people implement algorithms as objects, which act on the data objects ( in the previous case linalg could very well be a class with a method called svd ).
I have an example here to make my question clear. Suppose my data object is called sample and my algorithm is called rasam.
I can implement rasam as a method of my sample object and access it as
this_sample = sample()
this_sample.rasam(rasam_args)
or
this_sample = sample()
rasam(this_sample,rasam_args)
or
this_sample = sample()
this_rasam = rasam(rasam_args)
this_rasam.run(this_sample)
Which of the above options would be considered a code good design? As a follow up why is the SVD algorithm implemented as a function where as SUM implemented as a method of the numpy array object?
I found this link that sort of addresses this question in a general "functions vs methods" point of view, but I am more interested in this specific context of data objects and algorithms that act on the data.
In Python, when should I use a function instead of a method?