Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'parameters': 0.04; 'yet.': 0.04; 'subject:Python': 0.06; 'assign': 0.07; 'subject:file': 0.07; '"__main__":': 0.09; '"if': 0.09; '__name__': 0.09; 'clause': 0.09; 'main()': 0.09; 'parsing': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'suggest': 0.14; '"global"': 0.16; 'class)': 0.16; 'clause.': 0.16; 'definitions,': 0.16; 'globals': 0.16; 'main():': 0.16; 'mutable': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'refactoring': 0.16; 'top-level': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'module': 0.19; 'file,': 0.19; 'command': 0.22; 'import': 0.22; 'separate': 0.22; 'tests': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; 'error': 0.23; 'test.': 0.24; 'options': 0.25; 'script': 0.25; 'defined': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'statement': 0.30; 'code': 0.31; 'file': 0.32; 'skip:_ 10': 0.34; 'except': 0.35; 'definition': 0.35; 'but': 0.35; 'module.': 0.36; 'hi,': 0.36; 'application': 0.37; 'level': 0.37; 'same.': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'anything': 0.39; '12,': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'here:': 0.62; 'email addr:gmail.com': 0.63; 'side': 0.67; 'yourself': 0.78; '2015': 0.84; 'batchelder': 0.84; 'received:fios.verizon.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Python file structure Date: Tue, 12 May 2015 17:34:32 -0400 References: <026e6357-917c-4d50-b70f-70903aa0e065@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-98-114-97-173.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 In-Reply-To: <026e6357-917c-4d50-b70f-70903aa0e065@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1431466492 news.xs4all.nl 2933 [2001:888:2000:d::a6]:42965 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90497 On 5/12/2015 3:49 PM, Ned Batchelder wrote: > On Tuesday, May 12, 2015 at 3:13:32 PM UTC-4, zljubi...@gmail.com wrote: >> Hi, I have python file with the following structure: >> >> import... >> >> A = configparser.get(...) >> B = configparser.get(...) >> >> Command line parameters parsing [they can change variable A or B] >> >> Def usage() >> Print how to use script parameters >> >> def main(): >> ... >> >> if __name__ == "__main__": >> main() >> >> If I find an error in command line parameters section I cannot call function usage() because it is not defined yet. >> >> I have few options here: >> 1. Put definition of usage function before command line parameters parsing section >> 2. Make parameters global and put them in the main function >> 3. ...maybe some other options... >> > > I would put all of the code into a function some place. Don't have > anything at the top level of the file except imports, function (and > class) definitions, and an "if __name__....." clause at the bottom. I was about to suggest the same. One advantage of 'write tests first' is that you force yourself to write testable code from the beginning, instead of refactoring later. > If you need to use globals, assign them inside a parse_arguments > function that has a "global" statement in it. Better not to have mutable module globals if you can avoid it. If you want application globals, put them in a separate module. > As a side note, if you are going to have code at the top-level of > the file, then there's no point in the "if __name__..." clause. > That clause is designed to make a file both runnable and importable. > But your top-level code makes the file very difficult to import. And you need to import to test. -- Terry Jan Reedy