On Fri, 16 Aug 2013 12:44:54 +1000 Steven D'Aprano email@example.com wrote:
On 16/08/13 04:10, Eric V. Smith wrote:
I agree with Mark: the proposed median, median.low, etc., doesn't feel right. Is there any example of doing this in the stdlib?
The most obvious case is datetime: we have datetime(), and datetime.now(), datetime.today(), and datetime.strftime(). The only API difference between it and median is that datetime is a type and median is not, but that's a difference that makes no difference:
Of course it does. The datetime classmethods return datetime instances, which is why it makes sense to have them classmethods (as opposed to module functions).
The median functions, however, don't return median instances.
My preference is to make median a singleton instance with a __call__ method, and the other flavours regular methods. Although I don't like polluting the global namespace with an unnecessary class that will only be instantiated once, if it helps I can do this:
class _Median: def __call__(self, data): ... def low(self, data): ...
median = _Median()
If that standard OOP design is unacceptable, I will swap the dots for underscores, but I won't like it.
Using "OOP design" for something which is conceptually not OO (you are just providing callables in the end, not types and objects: your _Median "type" doesn't carry any state) is not really standard in Python. It would be in Java :-)