Choose: class with static methods or module with functions

Chris Rebert clp2 at rebertia.com
Thu Apr 16 14:53:13 EDT 2009


On Thu, Apr 16, 2009 at 9:55 AM,  <bearophileHUGS at lycos.com> wrote:
> Ravi:
>> Which is a better approach.
>> My personal view is that I should create a module with functions.
>
> When in doubt, use the simplest solution that works well enough. In
> this case, module functions are simple and probably enough.
>
> But there can be a situation where you want to keep functions even
> closer, for example because in a module you have two classes and two
> groups of functions related to each class. In such situation
> staticmethods seem better.

Using staticmethods also allows you to override some of functions by
subclassing if need be.
Usually that's not necessary and so plain module functions are
preferred, but I know the std lib uses staticmethods in a few places
for this reason, so it occasionally is useful to do it this way.

Cheers,
Chris
-- 
I have a blog:
http://blog.rebertia.com



More information about the Python-list mailing list