Organisation of python classes and their methods
steve+comp.lang.python at pearwood.info
Sat Nov 3 02:06:45 CET 2012
On Fri, 02 Nov 2012 07:16:09 +0100, Martin Hewitson wrote:
> I'm beginning a large Python project which contains many packages,
> modules and classes. The organisation of those is clear to me.
> I don't like having source files with
> 100's of lines of code in, let alone 1000's.
Why? Do you do your software development on an iPhone?
Hundreds of lines is nothing, especially if you are including docstrings
and comments in that count. My Python startup file is over 100 lines, and
it is trivial.
100 lines is approximately and a half pages using a 10pt font size
(depending on the font and the platform, of course). In any case, it's
not a lot. If you average two lines of code every comment, blank line or
docstring, 100 lines is only 66 lines of actual code.
To give you an example of what I consider pushing the limit of how much
code should go into a single file, look at the source code to decimal.py.
In Python 3.2, that file is 6249 lines. There are:
- 2880 lines of actual code
- 2070 lines in docstrings, including blanks
- 606 commented lines
- 693 blank lines outside of docstrings
The module includes 20 top-level functions, 18 classes, and 213 methods.
Most of those methods are in just two classes, Context and Decimal, with
76 and 117 methods respectively. So there is an average of 12 lines of
code per function or method.
I wouldn't like to have to deal with a single file twice that size, but
decimal.py is relatively easy to deal with. It's not a trivial module by
any means, but nor is it especially over-complex. The complexity is all
in the computations, not the code layout.
Please understand me -- I'm not saying that you should stuff all your
code into a single module. Code should only be placed into a single
module when the code is related. But in my opinion, you should not split
a logical single unit of code, a module of related code, into separate
files just because it is "too big" until it is at least as large as
More information about the Python-list