Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12357

Re: about if __name == '__main__':

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.016
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'decent': 0.07; 'python': 0.08; 'executed': 0.09; "module's": 0.09; 'variables,': 0.09; 'definition,': 0.16; 'namespace.': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.16; 'appropriate.': 0.18; 'example.': 0.18; 'cc:no real name:2**0': 0.20; 'seems': 0.20; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'pm,': 0.24; 'command': 0.24; 'aug': 0.24; 'besides': 0.24; 'function': 0.27; 'fact': 0.27; 'problem': 0.28; 'message-id:@mail.gmail.com': 0.29; 'script': 0.29; 'cc:addr:python.org': 0.30; 'imported': 0.30; 'sun,': 0.30; 'received:209.85.161.46': 0.31; 'received:mail- fx0-f46.google.com': 0.31; 'usual': 0.31; 'certainly': 0.32; 'actual': 0.32; 'anyone': 0.32; 'it.': 0.33; 'there': 0.33; 'clearly': 0.34; 'another.': 0.34; 'which,': 0.34; 'entry': 0.35; 'received:209.85.161': 0.35; 'problem.': 0.36; 'statements': 0.37; 'run': 0.37; 'but': 0.37; 'two': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'called': 0.40; 'your': 0.61; 'bottom': 0.64; '12:56': 0.84; 'descriptive': 0.84; 'isolated': 0.84; 'technique': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=8UWPNjIx3xsKMYAS/N+StFjB7kUN4xNlFQZshNTxAcA=; b=Y9kpBKf7KezKJzJ298Lh5LRXlln5odA1Ivc7gQxzQbBAtMp/u9xOMAqE2cqfbEdrki QDh7Z32bO4dWxloV3p+8lBs3sMA4EBDeiw8jtRRsnWTb8fLX8oWfCApCNmGmlWu9xdkO 0bKbBK1hx8vOPf5dEl1OiJGexdDHWTwrqb5/g=
MIME-Version 1.0
In-Reply-To <5dde58ef-f68e-4e62-a2c8-125bf6c35270@x11g2000prk.googlegroups.com>
References <c0025c7f-8fb1-4be6-a41b-6227aa388a53@h23g2000pra.googlegroups.com> <mailman.509.1314550323.27778.python-list@python.org> <5dde58ef-f68e-4e62-a2c8-125bf6c35270@x11g2000prk.googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sun, 28 Aug 2011 17:55:39 -0600
Subject Re: about if __name == '__main__':
To woooee <woooee@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.519.1314575771.27778.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1314575771 news.xs4all.nl 2543 [2001:888:2000:d::a6]:57423
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12357

Show key headers only | View raw


On Sun, Aug 28, 2011 at 12:56 PM, woooee <woooee@gmail.com> wrote:
> Two main routines, __main__ and main(), is not the usual or the common
> way to do it.  It is confusing and anyone looking at the end of the
> program for statements executed when the program is called will find
> an isolated call to main(), and then have to search the program for
> the statements that should have been at the bottom of the program.

Not a problem if you write them one after another.  Besides which, any
decent editor will have a command to go directly from the function
call to the function definition, so I really don't see this as a
problem.

> The only reason to use such a technique in Python is if you want to
> call the function if the program is run from the command line, and
> also call the same function if the program is imported from another.

Or if your main "routine" has variables, and you don't particularly
want them polluting the module's global namespace.

> In which case, use a name that is descriptive, not "main".

"main" is descriptive in that it clearly designates the entry point of
the script to anybody accustomed to such terminology.  But there is
certainly no reason it needs to be "main", and in fact I only used
that for the example.  In an actual script I would use whatever seems
appropriate.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

about  if __name == '__main__': Amit Jaluf <amitjaluf@gmail.com> - 2011-08-28 08:34 -0700
  Re: about if __name == '__main__': Ian Kelly <ian.g.kelly@gmail.com> - 2011-08-28 10:51 -0600
    Re: about if __name == '__main__': woooee <woooee@gmail.com> - 2011-08-28 11:56 -0700
      Re: about if __name == '__main__': Terry Reedy <tjreedy@udel.edu> - 2011-08-28 17:35 -0400
      Re: about if __name == '__main__': Ian Kelly <ian.g.kelly@gmail.com> - 2011-08-28 17:55 -0600
      Re: about if __name == '__main__': Cameron Simpson <cs@zip.com.au> - 2011-08-29 09:59 +1000
    Re: about if __name == '__main__': Amit Jaluf <amitjaluf@gmail.com> - 2011-08-28 20:42 -0700

csiph-web