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


Groups > comp.lang.python > #12346

Re: about if __name == '__main__':

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'sys': 0.05; 'defines': 0.07; 'definitions': 0.07; 'script,': 0.07; 'python': 0.08; '__name__': 0.09; 'be:': 0.09; 'executed': 0.09; 'executes': 0.09; 'url:faq': 0.09; 'am,': 0.12; 'def': 0.15; 'library': 0.15; "'__main__':": 0.16; 'confused.': 0.16; 'executed.': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.16; 'cheers,': 0.18; 'arguments': 0.18; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'runs': 0.23; 'module,': 0.23; 'parse': 0.23; 'testing': 0.24; 'aug': 0.24; 'statement': 0.25; 'tests': 0.25; 'classes': 0.28; 'import': 0.28; 'pass': 0.29; 'module.': 0.29; 'message-id:@mail.gmail.com': 0.29; 'script': 0.29; 'cc:addr:python.org': 0.30; 'module': 0.30; 'imported': 0.30; 'loads': 0.30; 'modules,': 0.30; 'normal.': 0.30; 'sun,': 0.30; 'received:209.85.161.46': 0.31; 'received:mail- fx0-f46.google.com': 0.31; 'usual': 0.31; 'it.': 0.33; 'normally': 0.34; 'function.': 0.34; 'running': 0.35; 'received:209.85.161': 0.35; 'google': 0.36; 'starting': 0.36; 'file': 0.36; 'url:python': 0.36; 'question': 0.36; 'opposed': 0.37; 'languages': 0.37; 'perform': 0.37; 'some': 0.38; 'received:google.com': 0.38; 'url:org': 0.38; 'received:209.85': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'sets': 0.39; 'url:docs': 0.39; 'define': 0.39; "it's": 0.40; 'here': 0.65; 'idiom': 0.84
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=cIGsvApmxP2YDJSrPnpx1d0+/8BldMaKzPYypauQcSc=; b=HHCQ+S8IbxgPreEjMM/VEKSCWsTJAmGVtj78+EBOB4MN5YQKmGjo9i9xk9BWWTLTLZ aOnNymPylEHKigHVYDlOrhTL5ED7K3P318g9XpDi92A8pyekI7gXuH0lZHHlodyx8L1Y NW0YzHtkJsq80cW85CjKwoo/Ru+Y6dM/43BVs=
MIME-Version 1.0
In-Reply-To <c0025c7f-8fb1-4be6-a41b-6227aa388a53@h23g2000pra.googlegroups.com>
References <c0025c7f-8fb1-4be6-a41b-6227aa388a53@h23g2000pra.googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sun, 28 Aug 2011 10:51:32 -0600
Subject Re: about if __name == '__main__':
To Amit Jaluf <amitjaluf@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.509.1314550323.27778.python-list@python.org> (permalink)
Lines 46
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1314550323 news.xs4all.nl 2444 [2001:888:2000:d::a6]:60674
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12346

Show key headers only | View raw


On Sun, Aug 28, 2011 at 9:34 AM, Amit Jaluf <amitjaluf@gmail.com> wrote:
> hello group
>
> i have one question about this
>
> if __name == '__main__':

First, it should be:

if __name__ == '__main__':

> is it same as other languages like[c,c++]  main function. because of i
> google and read faqs
> and also " http://docs.python.org/faq/programming#how-do-i-find-the-current-module-name"
> this and i am confused.

No, that is not a main function.  It's not even a function.  When
Python runs a script, it loads that script as a module, sets its name
to be __main__, and then executes the entire module, starting from the
top as normal.  What that if statement defines is an ordinary branch
that is only executed if the current module is the main module, as
opposed to having been imported from some other module.  Normally this
will be at the end of the file so that all the definitions in the file
will have already been executed.

The usual idiom for this is:

def main(argv):
    # parse arguments and begin program logic...
    pass

if __name__ == '__main__':
    import sys
    main(sys.argv)

This is also frequently used for unit testing of library modules, so
that the module can be tested just by running it.

# define library classes and functions here

if __name__ == '__main__':
    # perform unit tests

Cheers,
Ian

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