Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 8f1462e52e1797a02c97073eed0b7f92 > files > 974

python-docs-2.6.5-2.5mdv2010.2.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>8. Top-level components &mdash; Python v2.6.5 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.6.5',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.5 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.6.5 documentation" href="../index.html" />
    <link rel="up" title="The Python Language Reference" href="index.html" />
    <link rel="next" title="9. Full Grammar specification" href="grammar.html" />
    <link rel="prev" title="7. Compound statements" href="compound_stmts.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="grammar.html" title="9. Full Grammar specification"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="compound_stmts.html" title="7. Compound statements"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.5 documentation</a> &raquo;</li>

          <li><a href="index.html" accesskey="U">The Python Language Reference</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="top-level-components">
<span id="top-level"></span><h1>8. Top-level components<a class="headerlink" href="#top-level-components" title="Permalink to this headline">¶</a></h1>
<p id="index-1062">The Python interpreter can get its input from a number of sources: from a script
passed to it as standard input or as program argument, typed in interactively,
from a module source file, etc.  This chapter gives the syntax used in these
cases.</p>
<div class="section" id="complete-python-programs">
<span id="programs"></span><h2>8.1. Complete Python programs<a class="headerlink" href="#complete-python-programs" title="Permalink to this headline">¶</a></h2>
<span class="target" id="index-1063"></span><p id="index-1064">While a language specification need not prescribe how the language interpreter
is invoked, it is useful to have a notion of a complete Python program.  A
complete Python program is executed in a minimally initialized environment: all
built-in and standard modules are available, but none have been initialized,
except for <a title="Access system-specific parameters and functions." class="reference external" href="../library/sys.html#module-sys"><tt class="xref docutils literal"><span class="pre">sys</span></tt></a> (various system services), <a title="The module that provides the built-in namespace." class="reference external" href="../library/__builtin__.html#module-__builtin__"><tt class="xref docutils literal"><span class="pre">__builtin__</span></tt></a> (built-in
functions, exceptions and <tt class="xref docutils literal"><span class="pre">None</span></tt>) and <a title="The environment where the top-level script is run." class="reference external" href="../library/__main__.html#module-__main__"><tt class="xref docutils literal"><span class="pre">__main__</span></tt></a>.  The latter is used to
provide the local and global namespace for execution of the complete program.</p>
<p>The syntax for a complete Python program is that for file input, described in
the next section.</p>
<p id="index-1065">The interpreter may also be invoked in interactive mode; in this case, it does
not read and execute a complete program but reads and executes one statement
(possibly compound) at a time.  The initial environment is identical to that of
a complete program; each statement is executed in the namespace of
<a title="The environment where the top-level script is run." class="reference external" href="../library/__main__.html#module-__main__"><tt class="xref docutils literal"><span class="pre">__main__</span></tt></a>.</p>
<p id="index-1066">Under Unix, a complete program can be passed to the interpreter in three forms:
with the <a class="reference external" href="../using/cmdline.html#cmdoption-c"><em class="xref">-c</em></a> <em>string</em> command line option, as a file passed as the
first command line argument, or as standard input. If the file or standard input
is a tty device, the interpreter enters interactive mode; otherwise, it executes
the file as a complete program.</p>
</div>
<div class="section" id="file-input">
<span id="id1"></span><h2>8.2. File input<a class="headerlink" href="#file-input" title="Permalink to this headline">¶</a></h2>
<p>All input read from non-interactive files has the same form:</p>
<pre>
<strong id="grammar-token-file_input">file_input</strong> ::=  (NEWLINE | <a class="reference external" href="compound_stmts.html#grammar-token-statement"><tt class="xref docutils literal"><span class="pre">statement</span></tt></a>)*
</pre>
<p>This syntax is used in the following situations:</p>
<ul class="simple">
<li>when parsing a complete Python program (from a file or from a string);</li>
<li>when parsing a module;</li>
<li>when parsing a string passed to the <a class="reference external" href="simple_stmts.html#exec"><tt class="xref docutils literal"><span class="pre">exec</span></tt></a> statement;</li>
</ul>
</div>
<div class="section" id="interactive-input">
<span id="interactive"></span><h2>8.3. Interactive input<a class="headerlink" href="#interactive-input" title="Permalink to this headline">¶</a></h2>
<p>Input in interactive mode is parsed using the following grammar:</p>
<pre>
<strong id="grammar-token-interactive_input">interactive_input</strong> ::=  [<a class="reference external" href="compound_stmts.html#grammar-token-stmt_list"><tt class="xref docutils literal"><span class="pre">stmt_list</span></tt></a>] NEWLINE | <a class="reference external" href="compound_stmts.html#grammar-token-compound_stmt"><tt class="xref docutils literal"><span class="pre">compound_stmt</span></tt></a> NEWLINE
</pre>
<p>Note that a (top-level) compound statement must be followed by a blank line in
interactive mode; this is needed to help the parser detect the end of the input.</p>
</div>
<div class="section" id="expression-input">
<span id="id2"></span><h2>8.4. Expression input<a class="headerlink" href="#expression-input" title="Permalink to this headline">¶</a></h2>
<span class="target" id="index-1067"></span><p id="index-1068">There are two forms of expression input.  Both ignore leading whitespace. The
string argument to <a title="eval" class="reference external" href="../library/functions.html#eval"><tt class="xref docutils literal"><span class="pre">eval()</span></tt></a> must have the following form:</p>
<pre>
<strong id="grammar-token-eval_input">eval_input</strong> ::=  <a class="reference external" href="expressions.html#grammar-token-expression_list"><tt class="xref docutils literal"><span class="pre">expression_list</span></tt></a> NEWLINE*
</pre>
<p id="index-1069">The input line read by <a title="input" class="reference external" href="../library/functions.html#input"><tt class="xref docutils literal"><span class="pre">input()</span></tt></a> must have the following form:</p>
<pre>
<strong id="grammar-token-input_input">input_input</strong> ::=  <a class="reference external" href="expressions.html#grammar-token-expression_list"><tt class="xref docutils literal"><span class="pre">expression_list</span></tt></a> NEWLINE
</pre>
<p id="index-1070">Note: to read &#8216;raw&#8217; input line without interpretation, you can use the built-in
function <a title="raw_input" class="reference external" href="../library/functions.html#raw_input"><tt class="xref docutils literal"><span class="pre">raw_input()</span></tt></a> or the <tt class="xref docutils literal"><span class="pre">readline()</span></tt> method of file objects.</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="#">8. Top-level components</a><ul>
<li><a class="reference external" href="#complete-python-programs">8.1. Complete Python programs</a></li>
<li><a class="reference external" href="#file-input">8.2. File input</a></li>
<li><a class="reference external" href="#interactive-input">8.3. Interactive input</a></li>
<li><a class="reference external" href="#expression-input">8.4. Expression input</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="compound_stmts.html"
                                  title="previous chapter">7. Compound statements</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="grammar.html"
                                  title="next chapter">9. Full Grammar specification</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/reference/toplevel_components.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="grammar.html" title="9. Full Grammar specification"
             >next</a> |</li>
        <li class="right" >
          <a href="compound_stmts.html" title="7. Compound statements"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.5 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Language Reference</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2010, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Mar 19, 2010.
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.5.
    </div>

  </body>
</html>