[Tutor] sharing a small script someone may find of use

Cameron Simpson cs at cskk.id.au
Fri Aug 12 22:40:49 EDT 2022


On 13Aug2022 12:35, Cameron Simpson <cs at cskk.id.au> wrote:
>Your modules are a list. That involves a linear code to scan it.
>Admittedly, itis likely to be quite a short list, but a set works
>better. Earlier in the code:
>
>    modules = set()
>
>then the same test.
>
>    if module_name not in modules:
>        modules.add(module_name)

I had intended to include an example around "likely to be quite a short 
list". Because I happen to be plotting data this today and I've imported 
matplotlib. Now:

    >>> len(sorted(sys.modules.keys()))
    1381

Ouch!

Still, your modules is a list of names from source code, not the 
sys.modules dict. So probably small still.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list