[Python-ideas] start, test, init

spir denis.spir at gmail.com
Mon Dec 2 13:37:20 CET 2013


On 12/01/2013 10:13 PM, Nick Coghlan wrote:
> On 2 Dec 2013 06:34, "Ron Adam" <ron3200 at gmail.com> wrote:
>>
>>
>>
>> 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__.
>
> Oh, interesting. Such an option could also alter -O and -OO to keep assert
> statements.
>
> I'd love to see that idea elaborated further, as there are a variety of
> questions around how it might work in practice (Compile time constant like
> __debug__? Always False builtin shadowed in the main module? If it works
> like __debug__ and/or affects -O and -OO, does it need a new naming
> convention in the pycache directory?)

This is very similar to my proposal, except that you'd control that from the 
code. No need to change command-line args.

Denis


More information about the Python-ideas mailing list