On 07/05/2017 09:45 AM, Zachary Ware wrote: > On Wed, Jul 5, 2017 at 10:37 AM, David Rock <david.r...@gmail.com> wrote:
>> I personally find using main() cumbersome, but many examples I come >> across use main(). Is there some fundamental benefit to using main() >> that I'm missing? > > In no particular order: testing, encapsulation, and reusability. With > a "main()" function (which, recall, can be named whatever you like; it > doesn't have to be "main") you can directly call the function in your > tests to make sure it acts the way you want. The encapsulation of the > "main" code in a "main()" function also reduces your global state (and > makes global state a bit more difficult to use), which is usually a > good thing. And finally, it makes it possible to use the "main()" > function in some other piece of code that imports it. > As a vaguely contradictory position to a part of this (which I in the main agree with): if your objective is to make a module, and also have some code (perhaps including test code) which is run in the non-module (aka not-imported) case, then stuffing that code in a function main() means you've exposed that not-really-module-code as part of the module, and might then want to take steps to hide it from clients so it doesn't end up looking like part of the module API. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor