imaplib: is this really so unwieldy?

Michael Torrie torriem at gmail.com
Thu May 27 14:56:03 EDT 2021


On 5/27/21 10:42 AM, hw wrote:
> What do you do with it when importing it?  Do you somehow design your 
> programs as modules in some way that makes them usable as some kind of 
> library funktion?

Yes, precisely.  Typically I break up my python projects into logical
modules, which are each kind of like a library in their own right.  The
if __name__=="__main__" idiom is really handy for this. It lets me build
testing into each module. If I run the module directly, it can execute
tests on the functions in that module. If it's imported, the code inside
the if __name__=="__main__" block is not executed.  Sometimes I'll have
a python file that can run standalone, using command-line arguments, or
be imported by something else and used that.  All depends on my needs at
the moment, but this mechanism is very powerful.

Note that any py file is loaded and executed when imported.  So any
module-level initialization code gets run whether a file is imported or
run directly (which is why the if __name__=="__main__" idiom works).


More information about the Python-list mailing list