Organizing Python Code

Tres Seaver tseaver at starbase.neosoft.com
Wed Oct 4 10:13:15 EDT 2000


In article <39D82B17.81958D15 at esatclear.ie>,
Russell Wallace  <rwallace at esatclear.ie> wrote:
>Thomas Gagne wrote:
>> 
>> I'm trying to do a project with Python and I'm not convinced I've set things
>> up properly.  In writing C code I've grown accustomed to creating separate
>> files for each function.
>
>For each function, not for each module?  I wouldn't recommend this in
>any language, but whatever works for you.
>
>> This doesn't seem to really be the right thing to do
>> in Python since the top of each would include a bunch of "import" or "from"
>> statements.  On the other extreme, I don't like having really big files
>> either.
>> 
>> What's everyone else doing?  What's idiomatic?
>
>I do basically the same as in C.  I have one file per module.  Each of
>these has at the top:
>
>    from foo import *
>
>where foo is the name of my project.  Foo.py consists of:
>
>    from first import *
>    from second import *
>
>etc, where first, second etc are the names of the modules.  So each
>module can access what it needs without having to carry a lot of import
>statements around.

Talk about Esau "trading away his birthright for a mess of pottage"!

Urg!  You are trading away the scoping provided by Python's module
system, along with the clarity of specifiying your dependencies
explicitly, for the convenience of not having to do the imports
yourself (and of not having to type module prefixes).

Just as '#include "kitchensink.h"' reduces maintainability in a
C/C++ program (where 'kitchensink.h' then #includes all the other
project headers), your strategy is going to cause the future
maintainer of your code (likely, you!) to take your name in vain
after chasing down a few obscure buglets which it enables.

IMHO-of-course'ly,

Tres.
-- 
===============================================================
Tres Seaver                                tseaver at digicool.com
Digital Creations     "Zope Dealers"       http://www.zope.org



More information about the Python-list mailing list