Unification of Methods and Functions

Greg Ewing greg at cosc.canterbury.ac.nz
Mon May 24 23:44:07 EDT 2004


David MacQuigg wrote:
> The load method is unique to Bags, and it belongs
> in the Bag class.  To me this is a more fundamental consideration than
> whether or not the method uses instance variables.

Giving the function a name such as load_bag would be
just as good in my opinion, if you don't want to dedicate
a whole module to the Bag class.

But then again, I don't see that dedicating a module
to it would be such a bad thing, either.

Python has what is termed a "package", which is
a module containing other modules. You can use a
package to group together modules which encapsulate
related classes. If you have various collection
classes, e.g., you might have a hierarchy of names
like

   collections.bags.Bag # class
   collections.bags.load # function
   collections.sets.Set
   collections.sets.load

etc.

By the way, when I start writing a new Python program,
often I'm tempted to put several classes in a module, only
to regret it later as those classes grow and I end up
with a huge source file that's hard to navigate around
in. I'm coming to the view that it may be better to
plan on one-class-per-module from the beginning.

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg




More information about the Python-list mailing list