Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2.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; 'subject:Python': 0.06; '(so': 0.07; 'assign': 0.07; 'subject:file': 0.07; 'sys': 0.07; '"if': 0.09; '__name__': 0.09; 'clause': 0.09; 'executed': 0.09; 'main()': 0.09; 'prevents': 0.09; 'references,': 0.09; 'violates': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; "wouldn't": 0.14; '"from': 0.16; '"global"': 0.16; "'__main__':": 0.16; 'assignment.': 0.16; "chris'": 0.16; 'class)': 0.16; 'definitions,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'layout,': 0.16; 'main().': 0.16; 'main():': 0.16; 'sys.exit(0)': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'wed,': 0.18; 'fit': 0.20; 'import': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; 'instance,': 0.24; 'file.': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'ideal': 0.29; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; '13,': 0.31; 'description,': 0.31; 'prevention': 0.31; 'with,': 0.31; 'file': 0.32; 'stuff': 0.32; 'skip:_ 10': 0.34; 'maybe': 0.34; "can't": 0.35; 'advice': 0.35; 'except': 0.35; 'definition': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'consistent': 0.36; 'done,': 0.36; 'level': 0.37; 'anything': 0.39; 'does': 0.39; 'even': 0.60; 'most': 0.60; 'simple': 0.61; 'first': 0.61; 'talking': 0.65; 'statement,': 0.68; '2015': 0.84; 'batchelder': 0.84; 'to:none': 0.92; 'state.': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=b/kf6ZHsQlMAQc1dfyGB7ztJtlEp1SeIlWhH5TzfwpA=; b=JjjuPDUVHyujwQPx7xWaF3t02jJBDVeCNqoLh93BD5+mgWY2u9SUk5heaRxMRza2Zz r7TyJt9C886g2j9IjGrIa8tpsqtkOQISzsPruiwuFQPYg92O5ZoNzC3Im7qmjPrC/QTA dzZ8a5QJpHb74bjVKioeqdnwbxDMmFPSgedFhm/nEdaqsij3esyk0Yk/LysOLtoBxzC+ F2ExaPY+xquIxH/xlRXNmMtJWrMee0uf7264x8b49nMOyaC+tpAfua2Dp6Gvcdy2Gf3H GrMPArjWNMTp53VNMjOJQEhDWlUC+Qi3QHwbECyIjIoERzrZWsXPxhvs5cTbkIEmtVEM m+cQ== MIME-Version: 1.0 X-Received: by 10.50.141.164 with SMTP id rp4mr23808652igb.2.1431460962665; Tue, 12 May 2015 13:02:42 -0700 (PDT) In-Reply-To: <026e6357-917c-4d50-b70f-70903aa0e065@googlegroups.com> References: <026e6357-917c-4d50-b70f-70903aa0e065@googlegroups.com> Date: Wed, 13 May 2015 06:02:42 +1000 Subject: Re: Python file structure From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1431460970 news.xs4all.nl 2887 [2001:888:2000:d::a6]:34050 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90486 On Wed, May 13, 2015 at 5:49 AM, Ned Batchelder wrote: > 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. > > If you need to use globals, assign them inside a parse_arguments > function that has a "global" statement in it. > > This advice is consistent with Chris' "define things before they > are used." It does it by defining everything before anything is > run. Consistent with, yes, but not for the reason you state. I'm talking about code layout, not prevention of NameError. For instance, this would fit your description, and wouldn't error out, but wouldn't fit my ideal: import sys def main(): if len(sys.argv) < 2: usage() # do stuff def usage(): print("USAGE: programname arguments") sys.exit(0) if __name__ == '__main__': main() I would shift the definition of usage() up above main(). Even though both are executed before main() actually begins running, which prevents NameError, it's harder to skim the file. Obviously this is an ideal that can't always be attained (mutual references, for instance), but where it can be done, it means that the first instance of any token in a file is its definition - maybe in an import statement (so "from X import *" violates the principle, but I think most people avoid it anyway), or maybe in a def/class statement, or maybe a simple assignment. ChrisA