[Python-ideas] start, test, init

Ron Adam ron3200 at gmail.com
Sun Dec 1 21:32:52 CET 2013



On 12/01/2013 06:45 AM, spir wrote:
> # __main__ = start
> def start (args):
>      init()
>      # process args
>      ...
>
>
> What do you think? (bis)

I think you are describing good programming practices for larger modules. 
And I think most people who have been programming in python for any length 
of time develop some form of what you are describing.

The only difference is that they wouldn't need the conditional logic at the 
bottom of the module.  But that also serves as bit of self documentation as 
to what the module or script does, is more flexible, and only adds a line 
or two if the rest is put into functions or classes.


As for the testing case... I'd like for python to have a -t option that 
only sets a global name __test__ to True.  Then that covers most of your 
use cases without adding a whole lot.  Sometimes less is more.

    if __test__:
        test()
    elif __name__ == '__main__':
        main()

That's just one possible combination of using __test__ and __name__.

Cheers,
    Ron



More information about the Python-ideas mailing list