Just a quick question about main()
Grant Edwards
grant.b.edwards at gmail.com
Fri Oct 27 15:02:26 EDT 2017
On 2017-10-27, Chris Angelico <rosuav at gmail.com> wrote:
> On Sat, Oct 28, 2017 at 5:05 AM, ROGER GRAYDON CHRISTMAN <dvl at psu.edu> wrote:
>> While teaching my introductory course in Python, I occasionally see
>> submissions containing the following two program lines,[...]
>> if __name__ = '__main__':
>> ... main()
> If it's JUST for unit tests, I'd expect no main(), but instead to have
> it go straight into unittest.main(). IMO, the construct you show there
> implies three things:
>
> 1) This module is intended to be run from the command line
> 2) This module is intended to be imported by other modules
> 3) If imported by another module, this can also be invoked as if it
> were the top-level app.
I sometimes create a main function out of habit even if I can't
imagine a case #3.
A typical situation that I often encounter is that I write a set of
functions to perform some task(s) via a serial or network connection
using some industrial protocol. [For example, updating firmware in a
device.]
There are often two use cases:
1) It can be used from the command-line as a stand-alone application
with various command line options and arguments that specify the
operation[s] to be performed.
2) It can be imported by a GUI application in order to provide to the
GUI framework code the functions that can be called to do the
individual operations.
Even if the real-world end-user use case is purely the GUI one, it's
often far easier and faster to also include a main() for deveopment
and testing of the functions provided to the GUI.
--
Grant Edwards grant.b.edwards Yow! ONE LIFE TO LIVE for
at ALL MY CHILDREN in ANOTHER
gmail.com WORLD all THE DAYS OF
OUR LIVES.
More information about the Python-list
mailing list