how to arrange classes in .py files?

Terry Reedy tjreedy at
Fri Mar 27 02:54:23 CET 2009

Kent wrote:
> In above packages, each .py file contains one python class. And
> ClassName = Filename
> not sure about the right code structure of python oop.

Group related objects into one module.  People of course differ on 
lumping versus splitting.

> Can anyone give some hint on it? would be great with reason.

If classname == filename and you have lots of classes, then you have 
lots of entries in your directory and lots of import statements and lots 
of entries in sys.modules and the only way to group things on the disk 
is to add a directory, but there would still be no grouping of possibly 
100s of modules.

Also, Python has lots of object classes other than classes, and many of 
these other classes may reasonably be instanced in a module.

A modules is a namespace is a directory.  A class, by the way, is a 
namespace with behavior.  So the reason to group related things in a 
module may be similar to the reason to group related things in a class. 
  (Although I have read that some Java classes only 'group' one thing, 
which is generally silly from a Pythonic viewpoint.)


More information about the Python-list mailing list