I need to deal with two objects of a class in such a way as to return a third object of the same class, and I am trying to determine whether it is better to do this as an independent function that receives two objects and returns a third, or as a method that takes another object and returns a third .
For a simple example. Will it be:
from collections import namedtuple class Point(namedtuple('Point', 'x y')): __slots__ = ()
Or that:
from collections import namedtuple class Point(namedtuple('Point', 'x y')): __slots__ = ()
and why is one preferable to the other?
This seems much less clear than I expected when I asked the question.
Thus, it seems that something like a.midpoint (b) is not preferable, because it seems that a special place is taken by what is actually a symmetric function that returns a completely new point instance. But this, apparently, depends heavily on taste and style between something like an autonomous module function or a function attached to a class, but not intended to be called by an insance tool such as Point.midpoint (a, b).
I think, personally, I stylistically tend to autonomous functions of the module, but this may depend on the circumstances. In cases where a function is definitely tightly coupled to a class and there is a risk of namespace contamination or potential confusion, it is likely that creating a class function makes more sense.
In addition, several people mentioned that to make a function more general, perhaps by implementing additional class functions to support this. In this particular case of points and midpoints, this is probably the general best approach. It supports polymorphism and code reuse and is well readable. In many cases, however, this did not work (the project that inspired me to ask about this, for example), but the points and midpoints seemed to be a concise and understandable example to illustrate this question.
Thanks to everyone, it was instructive.