Path: csiph.com!usenet.pasdenom.info!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'parameters': 0.04; 'yet.': 0.04; 'subject:Python': 0.06; 'definitions': 0.07; 'subject:file': 0.07; 'parsing': 0.09; 'uses.': 0.09; '*only*': 0.16; '+1.': 0.16; 'assignments': 0.16; 'command-line': 0.16; "function's": 0.16; 'occurs,': 0.16; 'script,': 0.16; 'tends': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'wed,': 0.18; 'module': 0.19; 'possible,': 0.19; 'command': 0.22; 'error': 0.23; 'file.': 0.24; 'source': 0.25; 'options': 0.25; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'specifically': 0.29; 'chris': 0.29; 'am,': 0.29; 'generally': 0.29; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; '13,': 0.31; 'invoke': 0.31; 'probably': 0.32; "i'd": 0.34; 'something': 0.35; 'definition': 0.35; 'requirement': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'entry': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; '12,': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'helps': 0.61; 'strictly': 0.61; 'simple': 0.61; 'first': 0.61; 'here:': 0.62; 'high': 0.63; 'real': 0.63; 'such': 0.63; 'more': 0.64; '2015': 0.84; 'calls,': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=4ggAyBmBF3/WvbTMa+bHZ7yI3wNhf1eFK6SzcuYi2+c=; b=nB6PP/YFud/RtWGVdSR9UqO41oTIZ2iexVHCbUEkTjnv0Bmp1TlzYABuiVD7kc2ome bZcvAwdtg/tXEIOiBre3Q9QSoCCWBvwWJxiBhPZdp0KGkTKVU4g2N/MTnm6i2doR4PWa VrFSOchmWOajEm+byFFvaKvPUW3nYHWTo1t+xFd6VLEpUFGnkZ10uOhAyGQdmOPA8pne H9WuQSY2u5t59ZrbaGVJpTzAKKntePSNLKMYb7FL3sfxAbVVFZX4GUBAEF+RYkW+nipS koTE+8lc+1/pjHrJj/Epfa/Fx6hIieUtHFuu0UcM4oGbid2iHh6a4GEiCvdjzAFMseAw Bj8A== X-Received: by 10.42.188.141 with SMTP id da13mr5008195icb.94.1431460501461; Tue, 12 May 2015 12:55:01 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Tue, 12 May 2015 13:54:21 -0600 Subject: Re: Python file structure To: Python 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1431460510 news.xs4all.nl 2821 [2001:888:2000:d::a6]:53108 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90484 On Tue, May 12, 2015 at 1:29 PM, Chris Angelico wrote: > On Wed, May 13, 2015 at 5:13 AM, wrote: >> 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 > > I'd do this, unless there's a good reason not to. A simple usage > function probably doesn't have many dependencies, so it can logically > go high in the code. As a general rule, I like to organize code such > that things are defined higher up than they're used; it's not strictly > necessary (if they're used inside functions, the requirement is only > that they be defined before the function's called), but it helps with > clarity. That generally means that "def usage():" wants to go up above > any place where "usage()" occurs, but below the definitions of any > functions that usage() itself calls, and below the first assignments > to any global names it uses. It's not always possible, but when it is, > it tends to produce an easy-to-navigate source file. +1. Also, I like to put command-line parsing inside the main function and make that its *only* responsibility. The main function then calls the real entry point of my script, which will be something more specifically named. This also has the advantage that if some other module needs to invoke my script, all it has to do is call the entry point function which will be named something more suitable than "main".