WedWonder: Scripts and Modules

Barry Scott barry at barrys-emacs.org
Thu Sep 12 04:22:58 EDT 2019



> On 11 Sep 2019, at 21:24, 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?
> 
> (discounting distutils and similar installation tools, or unit testing methodology)
> 
> 
> There are over 500 questions on StackOverflow which refer to Python's
> 
> 	if __name__ == __main__:
> 
> construct. Even more if you include the idea of a main() multiple entry-point.
> 
> This construct enables code to distinguish between being "run" as a "script", and being imported as a "module". (which is not in question)
> 

In my mind this is a question about what side-effects of importing a module are
desireable and which are not.

A trivia script does not need the __name__ == '__main__' as its all about its side effects.

As scripts become more complex having it run on import might make debugging harder
and prevents reuse.

For example I will import a script at the REPL and examine it and call function in it to
help me understand and fix problems.  Having a __name__ == '__main__' is important
to allow this.

I often have modules that are part of a larger program that have their own main() functions
to unittest or give access to parsers etc.

In large projects with many modules import with side-effect can make for a maintenance
burden.

Barry


> -- 
> Regards,
> =dn
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 




More information about the Python-list mailing list