Re: Choose: class with static methods or module with functions

2009-04-17 Thread Dan Sommers
On Thu, 16 Apr 2009 09:55:40 -0700, bearophileHUGS wrote:

 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.

In order not to guess in the face of ambiguity, I propose that you handle 
that case (two classes and two groups of functions related to each 
class) by splitting that module into two modules.  One module would 
contain one of the classes and its related functions; the other module 
would contain the other class and its related functions.

Unless, of course, you mean that you have two groups of functions, each 
of which are related to *both* classes, in which case I'd still prefer 
module-level functions to staticmethods, but I'm old enough that I only 
speak Object Oriented as a Second Language.

Dan

-- 
Dan Sommers   A death spiral goes clock-
http://www.tombstonezero.net/dan/   wise north of the equator.
Atoms are not things. -- Werner Heisenberg  -- Dilbert's PHB

--
http://mail.python.org/mailman/listinfo/python-list


Re: Choose: class with static methods or module with functions

2009-04-16 Thread bearophileHUGS
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.

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Re: Choose: class with static methods or module with functions

2009-04-16 Thread Chris Rebert
On Thu, Apr 16, 2009 at 9:55 AM,  bearophileh...@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
--
http://mail.python.org/mailman/listinfo/python-list