{"id":1665,"date":"2012-07-29T11:37:41","date_gmt":"2012-07-29T01:37:41","guid":{"rendered":"http:\/\/www.somethinkodd.com\/oddthinking\/?p=1665"},"modified":"2012-07-30T03:46:28","modified_gmt":"2012-07-29T17:46:28","slug":"why-the-python-main-area-should-be-small","status":"publish","type":"post","link":"https:\/\/www.somethinkodd.com\/oddthinking\/2012\/07\/29\/why-the-python-main-area-should-be-small\/","title":{"rendered":"Why the Python Main area should be small"},"content":{"rendered":"<p>Today, I am justifying a particular personal coding standard rule. If I explicitly argue the case, I hope I will conform to it more.<\/p>\n<p>Consider a Python module:<\/p>\n<p><code><br \/>\nfrom blah import Blah<br \/>\nfrom foo import Foo<\/p>\n<p># Lots of useful declarations in this area.<br \/>\n#<br \/>\n# Traditionally, nothing that would actively run without being called,<br \/>\n# just declarations.<br \/>\n#<br \/>\n# Let's call it the Decl Area.<\/p>\n<p>if __name__ == '__main__':<br \/>\n\t# This code will only be seen if the package is directly run.<br \/>\n\t# Not if it is merely imported.<br \/>\n\t#<br \/>\n\t# Code that will actually run presumably calling the code in the<br \/>\n\t# Decl Area.<br \/>\n\t#<br \/>\n\t# Let's call this the Main Area.<br \/>\n<\/code><\/p>\n<p>Code in the Main Area suffers from <em>three<\/em> short-comings.<\/p>\n<ul>\n<li>It cannot be re-used by another module.<\/li>\n<li>It cannot be exercised by a unit test.<\/li>\n<li>It declares globals, and therefore should use the awkward\tGLOBALS_NAMING_STANDARD. Oh, and be indented, because it inside an <code>if<\/code> \tstatement.<\/li>\n<\/ul>\n<p>Conclusion: Move as much code as possible out of the main area and into the declaration area.<\/p>\n<p>Examples of what I am happy to leave inside the main area:<\/p>\n<ul>\n<li>Calls to unit-test framework for self-testing modules. The unit-tests themselves should not be here, so they can be called by others.<\/li>\n<li>Calls to functions to set the Window title (Windows only). That should not apply when the code is re-used.<\/li>\n<li>Calls to a <code>main()<\/code> function in the declaration area if this is  actually a script that does anything.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Today, I am justifying a particular personal coding standard rule. If I explicitly argue the case, I hope I will conform to it more.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_s2mail":"yes","footnotes":""},"categories":[34],"tags":[81],"class_list":["post-1665","post","type-post","status-publish","format-standard","hentry","category-software-development","tag-python"],"_links":{"self":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts\/1665","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/comments?post=1665"}],"version-history":[{"count":6,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts\/1665\/revisions"}],"predecessor-version":[{"id":1671,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts\/1665\/revisions\/1671"}],"wp:attachment":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/media?parent=1665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/categories?post=1665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/tags?post=1665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}