korsygfhrtzangaiide
Elepffwdsff
/
usr
/
share
/
doc
/
python-docs-2.7.5
/
html
/
library
/
Upload FileeE
HOME
<!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>31.3. symtable — Access to the compiler’s symbol tables — Python 2.7.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.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.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 2.7.5 documentation" href="../index.html" /> <link rel="up" title="31. Python Language Services" href="language.html" /> <link rel="next" title="31.4. symbol — Constants used with Python parse trees" href="symbol.html" /> <link rel="prev" title="31.2. ast — Abstract Syntax Trees" href="ast.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </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="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="symbol.html" title="31.4. symbol — Constants used with Python parse trees" accesskey="N">next</a> |</li> <li class="right" > <a href="ast.html" title="31.2. ast — Abstract Syntax Trees" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="language.html" accesskey="U">31. Python Language Services</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-symtable"> <span id="symtable-access-to-the-compiler-s-symbol-tables"></span><h1>31.3. <a class="reference internal" href="#module-symtable" title="symtable: Interface to the compiler's internal symbol tables."><tt class="xref py py-mod docutils literal"><span class="pre">symtable</span></tt></a> — Access to the compiler’s symbol tables<a class="headerlink" href="#module-symtable" title="Permalink to this headline">¶</a></h1> <p>Symbol tables are generated by the compiler from AST just before bytecode is generated. The symbol table is responsible for calculating the scope of every identifier in the code. <a class="reference internal" href="#module-symtable" title="symtable: Interface to the compiler's internal symbol tables."><tt class="xref py py-mod docutils literal"><span class="pre">symtable</span></tt></a> provides an interface to examine these tables.</p> <div class="section" id="generating-symbol-tables"> <h2>31.3.1. Generating Symbol Tables<a class="headerlink" href="#generating-symbol-tables" title="Permalink to this headline">¶</a></h2> <dl class="function"> <dt id="symtable.symtable"> <tt class="descclassname">symtable.</tt><tt class="descname">symtable</tt><big>(</big><em>code</em>, <em>filename</em>, <em>compile_type</em><big>)</big><a class="headerlink" href="#symtable.symtable" title="Permalink to this definition">¶</a></dt> <dd><p>Return the toplevel <a class="reference internal" href="#symtable.SymbolTable" title="symtable.SymbolTable"><tt class="xref py py-class docutils literal"><span class="pre">SymbolTable</span></tt></a> for the Python source <em>code</em>. <em>filename</em> is the name of the file containing the code. <em>compile_type</em> is like the <em>mode</em> argument to <a class="reference internal" href="functions.html#compile" title="compile"><tt class="xref py py-func docutils literal"><span class="pre">compile()</span></tt></a>.</p> </dd></dl> </div> <div class="section" id="examining-symbol-tables"> <h2>31.3.2. Examining Symbol Tables<a class="headerlink" href="#examining-symbol-tables" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="symtable.SymbolTable"> <em class="property">class </em><tt class="descclassname">symtable.</tt><tt class="descname">SymbolTable</tt><a class="headerlink" href="#symtable.SymbolTable" title="Permalink to this definition">¶</a></dt> <dd><p>A namespace table for a block. The constructor is not public.</p> <dl class="method"> <dt id="symtable.SymbolTable.get_type"> <tt class="descname">get_type</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.get_type" title="Permalink to this definition">¶</a></dt> <dd><p>Return the type of the symbol table. Possible values are <tt class="docutils literal"><span class="pre">'class'</span></tt>, <tt class="docutils literal"><span class="pre">'module'</span></tt>, and <tt class="docutils literal"><span class="pre">'function'</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="symtable.SymbolTable.get_id"> <tt class="descname">get_id</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.get_id" title="Permalink to this definition">¶</a></dt> <dd><p>Return the table’s identifier.</p> </dd></dl> <dl class="method"> <dt id="symtable.SymbolTable.get_name"> <tt class="descname">get_name</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.get_name" title="Permalink to this definition">¶</a></dt> <dd><p>Return the table’s name. This is the name of the class if the table is for a class, the name of the function if the table is for a function, or <tt class="docutils literal"><span class="pre">'top'</span></tt> if the table is global (<a class="reference internal" href="#symtable.SymbolTable.get_type" title="symtable.SymbolTable.get_type"><tt class="xref py py-meth docutils literal"><span class="pre">get_type()</span></tt></a> returns <tt class="docutils literal"><span class="pre">'module'</span></tt>).</p> </dd></dl> <dl class="method"> <dt id="symtable.SymbolTable.get_lineno"> <tt class="descname">get_lineno</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.get_lineno" title="Permalink to this definition">¶</a></dt> <dd><p>Return the number of the first line in the block this table represents.</p> </dd></dl> <dl class="method"> <dt id="symtable.SymbolTable.is_optimized"> <tt class="descname">is_optimized</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.is_optimized" title="Permalink to this definition">¶</a></dt> <dd><p>Return <tt class="docutils literal"><span class="pre">True</span></tt> if the locals in this table can be optimized.</p> </dd></dl> <dl class="method"> <dt id="symtable.SymbolTable.is_nested"> <tt class="descname">is_nested</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.is_nested" title="Permalink to this definition">¶</a></dt> <dd><p>Return <tt class="docutils literal"><span class="pre">True</span></tt> if the block is a nested class or function.</p> </dd></dl> <dl class="method"> <dt id="symtable.SymbolTable.has_children"> <tt class="descname">has_children</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.has_children" title="Permalink to this definition">¶</a></dt> <dd><p>Return <tt class="docutils literal"><span class="pre">True</span></tt> if the block has nested namespaces within it. These can be obtained with <a class="reference internal" href="#symtable.SymbolTable.get_children" title="symtable.SymbolTable.get_children"><tt class="xref py py-meth docutils literal"><span class="pre">get_children()</span></tt></a>.</p> </dd></dl> <dl class="method"> <dt id="symtable.SymbolTable.has_exec"> <tt class="descname">has_exec</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.has_exec" title="Permalink to this definition">¶</a></dt> <dd><p>Return <tt class="docutils literal"><span class="pre">True</span></tt> if the block uses <tt class="docutils literal"><span class="pre">exec</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="symtable.SymbolTable.has_import_star"> <tt class="descname">has_import_star</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.has_import_star" title="Permalink to this definition">¶</a></dt> <dd><p>Return <tt class="docutils literal"><span class="pre">True</span></tt> if the block uses a starred from-import.</p> </dd></dl> <dl class="method"> <dt id="symtable.SymbolTable.get_identifiers"> <tt class="descname">get_identifiers</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.get_identifiers" title="Permalink to this definition">¶</a></dt> <dd><p>Return a list of names of symbols in this table.</p> </dd></dl> <dl class="method"> <dt id="symtable.SymbolTable.lookup"> <tt class="descname">lookup</tt><big>(</big><em>name</em><big>)</big><a class="headerlink" href="#symtable.SymbolTable.lookup" title="Permalink to this definition">¶</a></dt> <dd><p>Lookup <em>name</em> in the table and return a <a class="reference internal" href="#symtable.Symbol" title="symtable.Symbol"><tt class="xref py py-class docutils literal"><span class="pre">Symbol</span></tt></a> instance.</p> </dd></dl> <dl class="method"> <dt id="symtable.SymbolTable.get_symbols"> <tt class="descname">get_symbols</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.get_symbols" title="Permalink to this definition">¶</a></dt> <dd><p>Return a list of <a class="reference internal" href="#symtable.Symbol" title="symtable.Symbol"><tt class="xref py py-class docutils literal"><span class="pre">Symbol</span></tt></a> instances for names in the table.</p> </dd></dl> <dl class="method"> <dt id="symtable.SymbolTable.get_children"> <tt class="descname">get_children</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.SymbolTable.get_children" title="Permalink to this definition">¶</a></dt> <dd><p>Return a list of the nested symbol tables.</p> </dd></dl> </dd></dl> <dl class="class"> <dt id="symtable.Function"> <em class="property">class </em><tt class="descclassname">symtable.</tt><tt class="descname">Function</tt><a class="headerlink" href="#symtable.Function" title="Permalink to this definition">¶</a></dt> <dd><p>A namespace for a function or method. This class inherits <a class="reference internal" href="#symtable.SymbolTable" title="symtable.SymbolTable"><tt class="xref py py-class docutils literal"><span class="pre">SymbolTable</span></tt></a>.</p> <dl class="method"> <dt id="symtable.Function.get_parameters"> <tt class="descname">get_parameters</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Function.get_parameters" title="Permalink to this definition">¶</a></dt> <dd><p>Return a tuple containing names of parameters to this function.</p> </dd></dl> <dl class="method"> <dt id="symtable.Function.get_locals"> <tt class="descname">get_locals</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Function.get_locals" title="Permalink to this definition">¶</a></dt> <dd><p>Return a tuple containing names of locals in this function.</p> </dd></dl> <dl class="method"> <dt id="symtable.Function.get_globals"> <tt class="descname">get_globals</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Function.get_globals" title="Permalink to this definition">¶</a></dt> <dd><p>Return a tuple containing names of globals in this function.</p> </dd></dl> <dl class="method"> <dt id="symtable.Function.get_frees"> <tt class="descname">get_frees</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Function.get_frees" title="Permalink to this definition">¶</a></dt> <dd><p>Return a tuple containing names of free variables in this function.</p> </dd></dl> </dd></dl> <dl class="class"> <dt id="symtable.Class"> <em class="property">class </em><tt class="descclassname">symtable.</tt><tt class="descname">Class</tt><a class="headerlink" href="#symtable.Class" title="Permalink to this definition">¶</a></dt> <dd><p>A namespace of a class. This class inherits <a class="reference internal" href="#symtable.SymbolTable" title="symtable.SymbolTable"><tt class="xref py py-class docutils literal"><span class="pre">SymbolTable</span></tt></a>.</p> <dl class="method"> <dt id="symtable.Class.get_methods"> <tt class="descname">get_methods</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Class.get_methods" title="Permalink to this definition">¶</a></dt> <dd><p>Return a tuple containing the names of methods declared in the class.</p> </dd></dl> </dd></dl> <dl class="class"> <dt id="symtable.Symbol"> <em class="property">class </em><tt class="descclassname">symtable.</tt><tt class="descname">Symbol</tt><a class="headerlink" href="#symtable.Symbol" title="Permalink to this definition">¶</a></dt> <dd><p>An entry in a <a class="reference internal" href="#symtable.SymbolTable" title="symtable.SymbolTable"><tt class="xref py py-class docutils literal"><span class="pre">SymbolTable</span></tt></a> corresponding to an identifier in the source. The constructor is not public.</p> <dl class="method"> <dt id="symtable.Symbol.get_name"> <tt class="descname">get_name</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.get_name" title="Permalink to this definition">¶</a></dt> <dd><p>Return the symbol’s name.</p> </dd></dl> <dl class="method"> <dt id="symtable.Symbol.is_referenced"> <tt class="descname">is_referenced</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.is_referenced" title="Permalink to this definition">¶</a></dt> <dd><p>Return <tt class="docutils literal"><span class="pre">True</span></tt> if the symbol is used in its block.</p> </dd></dl> <dl class="method"> <dt id="symtable.Symbol.is_imported"> <tt class="descname">is_imported</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.is_imported" title="Permalink to this definition">¶</a></dt> <dd><p>Return <tt class="docutils literal"><span class="pre">True</span></tt> if the symbol is created from an import statement.</p> </dd></dl> <dl class="method"> <dt id="symtable.Symbol.is_parameter"> <tt class="descname">is_parameter</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.is_parameter" title="Permalink to this definition">¶</a></dt> <dd><p>Return <tt class="docutils literal"><span class="pre">True</span></tt> if the symbol is a parameter.</p> </dd></dl> <dl class="method"> <dt id="symtable.Symbol.is_global"> <tt class="descname">is_global</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.is_global" title="Permalink to this definition">¶</a></dt> <dd><p>Return <tt class="docutils literal"><span class="pre">True</span></tt> if the symbol is global.</p> </dd></dl> <dl class="method"> <dt id="symtable.Symbol.is_declared_global"> <tt class="descname">is_declared_global</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.is_declared_global" title="Permalink to this definition">¶</a></dt> <dd><p>Return <tt class="docutils literal"><span class="pre">True</span></tt> if the symbol is declared global with a global statement.</p> </dd></dl> <dl class="method"> <dt id="symtable.Symbol.is_local"> <tt class="descname">is_local</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.is_local" title="Permalink to this definition">¶</a></dt> <dd><p>Return <tt class="docutils literal"><span class="pre">True</span></tt> if the symbol is local to its block.</p> </dd></dl> <dl class="method"> <dt id="symtable.Symbol.is_free"> <tt class="descname">is_free</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.is_free" title="Permalink to this definition">¶</a></dt> <dd><p>Return <tt class="docutils literal"><span class="pre">True</span></tt> if the symbol is referenced in its block, but not assigned to.</p> </dd></dl> <dl class="method"> <dt id="symtable.Symbol.is_assigned"> <tt class="descname">is_assigned</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.is_assigned" title="Permalink to this definition">¶</a></dt> <dd><p>Return <tt class="docutils literal"><span class="pre">True</span></tt> if the symbol is assigned to in its block.</p> </dd></dl> <dl class="method"> <dt id="symtable.Symbol.is_namespace"> <tt class="descname">is_namespace</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.is_namespace" title="Permalink to this definition">¶</a></dt> <dd><p>Return <tt class="docutils literal"><span class="pre">True</span></tt> if name binding introduces new namespace.</p> <p>If the name is used as the target of a function or class statement, this will be true.</p> <p>For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">table</span> <span class="o">=</span> <span class="n">symtable</span><span class="o">.</span><span class="n">symtable</span><span class="p">(</span><span class="s">"def some_func(): pass"</span><span class="p">,</span> <span class="s">"string"</span><span class="p">,</span> <span class="s">"exec"</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">table</span><span class="o">.</span><span class="n">lookup</span><span class="p">(</span><span class="s">"some_func"</span><span class="p">)</span><span class="o">.</span><span class="n">is_namespace</span><span class="p">()</span> <span class="go">True</span> </pre></div> </div> <p>Note that a single name can be bound to multiple objects. If the result is <tt class="docutils literal"><span class="pre">True</span></tt>, the name may also be bound to other objects, like an int or list, that does not introduce a new namespace.</p> </dd></dl> <dl class="method"> <dt id="symtable.Symbol.get_namespaces"> <tt class="descname">get_namespaces</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.get_namespaces" title="Permalink to this definition">¶</a></dt> <dd><p>Return a list of namespaces bound to this name.</p> </dd></dl> <dl class="method"> <dt id="symtable.Symbol.get_namespace"> <tt class="descname">get_namespace</tt><big>(</big><big>)</big><a class="headerlink" href="#symtable.Symbol.get_namespace" title="Permalink to this definition">¶</a></dt> <dd><p>Return the namespace bound to this name. If more than one namespace is bound, a <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> is raised.</p> </dd></dl> </dd></dl> </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 internal" href="#">31.3. <tt class="docutils literal"><span class="pre">symtable</span></tt> — Access to the compiler’s symbol tables</a><ul> <li><a class="reference internal" href="#generating-symbol-tables">31.3.1. Generating Symbol Tables</a></li> <li><a class="reference internal" href="#examining-symbol-tables">31.3.2. Examining Symbol Tables</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="ast.html" title="previous chapter">31.2. <tt class="docutils literal"><span class="pre">ast</span></tt> — Abstract Syntax Trees</a></p> <h4>Next topic</h4> <p class="topless"><a href="symbol.html" title="next chapter">31.4. <tt class="docutils literal"><span class="pre">symbol</span></tt> — Constants used with Python parse trees</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/library/symtable.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" /> <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="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="symbol.html" title="31.4. symbol — Constants used with Python parse trees" >next</a> |</li> <li class="right" > <a href="ast.html" title="31.2. ast — Abstract Syntax Trees" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="language.html" >31. Python Language Services</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, 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 Jul 03, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>