Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #90486
| 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 | <rosuav@gmail.com> |
| 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 | <f25aa9d4-4025-457d-8072-5327c98db1bd@googlegroups.com> <026e6357-917c-4d50-b70f-70903aa0e065@googlegroups.com> |
| Date | Wed, 13 May 2015 06:02:42 +1000 |
| Subject | Re: Python file structure |
| From | Chris Angelico <rosuav@gmail.com> |
| Cc | "python-list@python.org" <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 <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.412.1431460970.12865.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
On Wed, May 13, 2015 at 5:49 AM, Ned Batchelder <ned@nedbatchelder.com> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Python file structure zljubisicmob@gmail.com - 2015-05-12 12:13 -0700
Re: Python file structure Chris Angelico <rosuav@gmail.com> - 2015-05-13 05:29 +1000
Re: Python file structure Ned Batchelder <ned@nedbatchelder.com> - 2015-05-12 12:49 -0700
Re: Python file structure zljubisicmob@gmail.com - 2015-05-12 12:58 -0700
Re: Python file structure Dave Angel <davea@davea.name> - 2015-05-12 16:43 -0400
Re: Python file structure Chris Angelico <rosuav@gmail.com> - 2015-05-13 06:02 +1000
Re: Python file structure Terry Reedy <tjreedy@udel.edu> - 2015-05-12 17:34 -0400
Re: Python file structure Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-12 13:54 -0600
Re: Python file structure Chris Angelico <rosuav@gmail.com> - 2015-05-13 06:07 +1000
Re: Python file structure Tim Chase <python.list@tim.thechases.com> - 2015-05-13 13:34 -0500
Re: Python file structure Chris Angelico <rosuav@gmail.com> - 2015-05-14 12:03 +1000
csiph-web