Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!multikabel.net!newsfeed20.multikabel.net!news2.euro.net!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'main()': 0.05; 'python': 0.08; 'scripts': 0.09; '__name__': 0.09; 'imho.': 0.09; "module's": 0.09; 'structure.': 0.09; 'top-level': 0.09; 'am,': 0.12; 'received:209.85.210.174': 0.13; 'received:mail- iy0-f174.google.com': 0.13; '"__main__":': 0.16; '.py': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'function.)': 0.16; 'main().': 0.16; 'syntactic': 0.16; 'subject:question': 0.17; 'wed,': 0.17; 'wrote:': 0.18; '(which': 0.19; 'seems': 0.20; 'appropriate': 0.22; "doesn't": 0.22; 'header :In-Reply-To:1': 0.22; 'module,': 0.23; 'fine': 0.24; 'module': 0.26; 'function': 0.27; 'import': 0.27; 'work.': 0.28; 'message- id:@mail.gmail.com': 0.28; 'module.': 0.29; 'nov': 0.29; 'imported': 0.30; 'chris': 0.30; 'quite': 0.32; "won't": 0.33; 'done': 0.34; 'to:addr:python-list': 0.34; 'calling': 0.34; 'anything': 0.34; 'end.': 0.34; 'routine': 0.34; 'short,': 0.34; 'however,': 0.36; 'file': 0.36; 'beginning': 0.36; 'instead,': 0.37; 'but': 0.37; 'run': 0.37; 'received:google.com': 0.37; 'some': 0.38; 'received:209.85': 0.38; 'i.e.': 0.39; 'either': 0.39; "it's": 0.40; 'received:209': 0.40; 'to:addr:python.org': 0.40; '2011': 0.61; 'your': 0.61; 'demand': 0.66; 'offer': 0.72; 'greatest': 0.73; '30,': 0.74; 'subject:Best': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=Jbt9Os5uucvEBp7STPOobU7Lyy4FWZA/fEYK32uZr2A=; b=Ng7e0xKWODs+VDV+fehc6HaN9/GDgHrvPmXmz9eXXjH5vGc5OxOb88SRwqeUIYR+KN 9vAE4GsegOZ/zpYhTh5xIBi6TElryFlJiKc1fiGnHmjJJ/49Tw+RbORXNAwSraAnP0FN 1WKKyPbbaY69gt2aIj+XTXVJwnad8ittDTlRg= MIME-Version: 1.0 In-Reply-To: References: Date: Wed, 30 Nov 2011 07:36:35 +1100 Subject: Re: Total newbie question: Best practice From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1322598998 news.xs4all.nl 6966 [2001:888:2000:d::a6]:60151 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16404 On Wed, Nov 30, 2011 at 7:06 AM, Colin Higwell wrote: > However, they are monolithic in nature; i.e. they begin at the beginning > and finish at the end. Having done a little reading, I note that it seems > to be quite common to have a function main() at the start (which in turn > calls other functions as appropriate), and then to call main() to do the > work. The reason for this practice is to allow your .py file to be either a top-level program or an imported module. if __name__ == "__main__": main() When you run a .py file directly, __name__ will be "__main__", and it'll execute main(). (Some programs directly embed the main routine in that if block - appropriate if main() would be very short, eg just calling some other function.) But if you import it as a module in some other program, that won't be the case; so instead, the module's functions are made available to the calling program. For simple scripts that don't have anything to offer as a module, it's fine to not bother with this structure. Python doesn't demand syntactic salt; that's one of its greatest features, IMHO. Chris Angelico