One module per class, bad idea?

Carl Banks pavlovevidence at gmail.com
Fri Dec 22 14:52:32 EST 2006


Kent Johnson wrote:
> Carl Banks wrote:
> > Now, I think this is the best way to use modules, but you don't need to
> > use modules to do get higher-level organization; you could use packages
> > instead.  It's a pain if you're working on two different classes in the
> > same system you have to keep switching files; but I guess some people
> > prefer to switch files rather than to scroll for some reason.
>
> That would be me. I strongly prefer to switch files rather than scroll.
> I use an editor that makes it easy to switch files. For me it is much
> easier to switch between files than to scroll between two parts of a
> file, and I don't lose my place when I switch back. I like to be able to
> see things side by side.

Man, I don't know you do it.

Say I'm sitting there concentrating on programming something, and I see
that I'll have to make a change in another file.  All of a sudden, I
have to recall some filename out of thin air.  Totally breaks my train
of thought, sometimes I space out trying to think of it because I have
to cold-start an entirely different part of my brain.  It's less of a
mental distraction to just scroll.

I'm in the habit of changing things as soon as the need arises.  If I'm
editing A and I see that I'll have to make a change in B, I stop
editing A, change B, then come back to A.  For me, it results in MUCH
fewer forgotten changes (YMMV).  But it also means a lot of switching.
Consequently, trying to minimize distraction during switches is
important to me.  Maybe it isn't if you switch less frequently, and
rarely while in the middle of somthing.  I wonder if there's any
correlation between how often one switches location, and preferrence
(tolerance) for file size.  I bet the more often one switches location,
the more likely they are to prefer larger files.

(BTW, any decent editor will let you view different positions of the
same file side-by-side.)


> So I do tend to put classes in separate modules. Not always - when two
> or more classes are closely related or a class has one or more helper
> classes they may share a module - but in general my major classes are
> each to a module.
>
> It does make the imports look funny - I tend to give the module the same
> name as the class, Java style, so I have
> from foo.bar.MyClass import MyClass
> but that is a minor point IMO.

I'd consider using all lowercase for the module.  Of course it doesn't
make sense to come up with a unique name for modules when they're
mostly one-to-one with classes, but it's still nice to give users a
clue whether they object they're looking at is a class or a module.


Carl Banks




More information about the Python-list mailing list