views:

274

answers:

6

I'm working on a Python web application in which I have some small modules that serve very specific functions: session.py, logger.py, database.py, etc. And by "small" I really do mean small; each of these files currently includes around 3-5 lines of code, or maybe up to 10 at most. I might have a few imports and a class definition or two in each. I'm wondering, is there any reason I should or shouldn't merge these into one module, something like misc.py?

My thoughts are that having separate modules helps with code clarity, and later on, if by some chance these modules grow to more than 10 lines, I won't feel so bad about having them separated. But on the other hand, it just seems like such a waste to have a bunch of files with only a few lines in each! And is there any significant difference in resource usage between the multi-file vs. single-file approach? (Of course I'm nowhere near the point where I should be worrying about resource usage, but I couldn't resist asking...)

I checked around to see whether this had been asked before and didn't see anything specific to Python, but if it's in fact a duplicate, I'd appreciate being pointed in the right direction.

+3  A: 

My thoughts are that having separate modules helps with code clarity, and later on, if by some chance these modules grow to more than 10 lines, I won't feel so bad about having them separated.

This. Keep it the way you have it.

Evan Fosmark
+1 Modules are the unit of reuse. If you could ever contemplate reusing it, keep it separate.
S.Lott
Importing a module causes a file seek/read operation, which is quite expensive. So there is a non trivial application startup cost to having modules. Unless distributing you code in zipped mode, having several hundred modules will cost you on the order of 2-5 seconds to import. ( more if you import from the network).
Kozyarchuk
"non trivial application startup cost to having modules"? Really? I'd never noticed. Do you have any measurements? Can you show this "non-trivial" cost? We have a web server that runs for days. Are you saying that the imports are measurable when compared with 100's of hours of running time?
S.Lott
Well, import cost would generally be in seconds even for larger apps and is usually at startup, so may not be as much of an issue for a long running process. But it could make a big difference if you are building a user launchable application or creating a subprocess to perform certain tasks in parallel.
Kozyarchuk
+3  A: 

Personally I find it easier to keep things like this in a single file, just for the practicality of editing a smaller number of files in my editor.

The important thing to do is treat the different pieces of code as though they were in separate files, so you ensure that you can trivially separate them later, for the reasons you cite. So for instance, don't introduce dependencies between the different pieces that will make it hard to disentangle them later.

RichieHindle
+1 That's an interesting way to look at it. Personally, if I'm going to treat pieces of code as being in separate files, I'd rather just have them in separate files. But it's sort of a personal opinion.
David Zaslavsky
+2  A: 

Off course you can have as many modules as you like.

But now let as think a little, what happens when we put every small code snippet into one single file.

We will end up in hundreds of import statements in any less trivial module. And off course you could also save a little by having all explicit in seperated files. But guess what: Nobody can remember so many module names and you might end up in searching for the right file anyway ...

I try to put things that belong together in one single file (unless it becomes to big!). But when I have small functions or classes that do not belong to other components in my system, I have "util" modules or the like. I also try to group these for example according to my application layering or seperate them by other means. One seperation criteria could be: Utilities that are used for UI and those that are not.

Juergen
+2  A: 

As a user of modules, I greatly prefer when I can include the entire module via a single import. Don't make a user of your package do multiple imports unless there's some reason to allow for importing different alternates.

BTW, there's no reason a single modules can't consist of multiple source files. The simplest case is to use an __init__.py file to simply load all the other code into the module's namespace.

Nelson
+1 This is my preference. Make it look to the user of the module to be one, but keep the implementation broken up, explicit, and easy to break apart.
Adam
A: 

Small.

Lennart Regebro
+1  A: 

For command line scripts there most likely will not be much difference unless each invocation invokes all files in the module, in which case there will be a slight performance cost as n files need to be opened vs one.

For mod_python there most likely will be no difference as byte-compiled modules stay alive for the duration of the apache process.

For google app engine though there will be a performance hit unless the service is constantly used and is "hot" as each cold start would require opening all files.

molicule