2

I'm currently writing a python script. It's currently about 400 lines long, and it seems like it's getting a bit long, as when I'm trying to scroll through it with eclipse it takes a bit to get from function A to function B. The structure is that it reads in a file, and then performs actions on what it reads in depending on the flags passed in by the user on the command line. Everything is more or less related (and in fact many functions call on other functions), so it makes sense in that regard to just leave everything in the same file.

I'm currently adding more features, or more things that can be put in for command line flags, and I'm wondering if there are any standards about how long a file should be before you start putting things into a new file. PEP 8 mentions the length of a line but nothing on the length of a file.

In summary: Are there any standards for how long a file should be before you just start throwing stuff into a new file?

2 Answers 2

7

I don't think you should break up the script just because of length. Any decent IDE should have the ability to collapse functions and/or skip to a specific function. There need to be good logical reason for seeing one part of it as functionally independent of the others. For example, if the function or functions for reading a file are generic enough, it might make sense to split them off into a separate module (file) which you then import. That way you can reuse that file reading module in a completely different program later, or make unit tests just for the file reading module, etc. Generally, I would say the following conditions should apply:

  1. The module does has some logical separate subfunction that it is doing.
  2. The module doesn't need to know anything about the script that is calling it (that isn't being passed as function parameters).

For example, the module should not check some global variable that is being set in the other script, or another module, or something like that.

1
  • Is there a common notation when it comes to creating modules for a project in python? Jun 29, 2021 at 6:29
3

By splitting code across separate files you're creating modules. Modules help you break up your code and keep it maintainable and reusable. From the python documentation

If you quit from the Python interpreter and enter it again, the definitions you have made (functions and variables) are lost. Therefore, if you want to write a somewhat longer program, you are better off using a text editor to prepare the input for the interpreter and running it with that file as input instead. This is known as creating a script. As your program gets longer, you may want to split it into several files for easier maintenance. You may also want to use a handy function that you’ve written in several programs without copying its definition into each program.

To support this, Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level...).

Try to keep interfaces between modules small so that you don't add unnecessary complexity to your program (you're trying to remove complexity by using modules). Also look at this Stack Overflow question

Not the answer you're looking for? Browse other questions tagged or ask your own question.