WedWonder: Scripts and Modules

Chris Angelico rosuav at gmail.com
Wed Sep 11 16:43:34 EDT 2019


On Thu, Sep 12, 2019 at 6:34 AM DL Neil via Python-list
<python-list at python.org> wrote:
>
> In this day-and-age do you have a script in live/production-use, which
> is also a module? What is the justification/use case?
>

Yes, absolutely. It's the easiest way to share code between two
scripts. Here's an example that I created recently:

https://github.com/Rosuav/shed/blob/master/BL1_find_items.py
https://github.com/Rosuav/shed/blob/master/BL2_find_items.py

These programs do similar jobs on very different formats of file, so
there's a small amount of common code and a large amount that isn't
common. One of the common sections is the FunctionArg class, which
ties in with argparse; it's implemented in BL1_find_items, and then
imported into BL2_find_items.

Of course I could break this out into its own dedicated file... but
why bother? It's not significant enough to warrant its own module, and
I don't see any value in an importable file of "all the stuff that I
might feel like importing"; it's just these two files that will need
this.

Basically, the script/module distinction is a convenient way to
simplify a common situation that doesn't need the overhead of anything
else. If the code starts getting used in lots more places, it'll
eventually get promoted to actual module.

ChrisA



More information about the Python-list mailing list