Block of code as a separate module in Python
Question: Assume I have a list `my_list`, a variable `var`, and a block of code that
modifies the list using the variable

    
    
    my_list = ['foo']
    var = 'bar'
    my_list.append(var)
    

In the actual task I have a lot of variables like `var` and a lot of commands
like `append` which modify the list. I want to relegate those commands to
another module. In the case at hand I would like to have two modules:
`modify.py` which contains the modifying commands

    
    
    my_list.append(var)
    

and `main.py` which defines the list and the variable and somehow uses the
code from the `modify.py`

    
    
    my_list = ['foo']
    var = 'bar'
    import_and_run modify
    

The goal is to make the main file more readable. Modifying commands in my case
can be nicely grouped and would really be good as separate modules. However, I
am only aware of the practice when one imports a function from a module, not a
block of code. I do not want to make the whole `modify.py` module a function
because

1) I don't want to pass all the arguments needed. Rather, I want `modify.py`
to directly have access to `main.py` name space.

2) code in `modify.py` is not really a function. It runs only once. Also, I do
not the whole module to be a body of a function, that just does not feel
right.

How do I achieve that? Or the whole attitude is wrong?


Answer: If your goal is to make the code more readable, I'd suggest taking these
steps.

  * Decompose your problem into a series of separate actions.
  * Give these actions names.
  * Define a function main in your module that calls functions named after the actions:

def main(): do_setp1() do_step2() # etc return

  * Separate you existing code into the functions that you're calling in main()

  * As @flaschbier suggested, collect related, common parameters into dictionaries to make passing the around easier to manage.

  * Consider repeating these steps on your new functions, decomposing them into sub-functions.

Done well, you should be left with a file that's easier to look at, because
the function definitions and their indented bodies break up the flow of text.

The code should be easier to reason about because you only need to understand
one function at a time, instead of the entire script.

Generally you want to keep all the code related to a particular task in a
single module, unless there's more than say 500 lines. But before moving code
into separate modules see if you can reduce the total lines of code by
factoring repeated code into functions, or making your code more succinct: for
example see if `for` loops can be replaced by list comprehensions.

Consider using code linting tools to help you make the code well-formatted.

So in summary: don't go against the grain of Python by hiding code in another
module and going down the `import_and_run` route. Instead use good code
organisation and Python's inherent good visual structure to make your code
readable.

