<!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 v2.6.6 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.6',
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.6 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.6 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. Abstract Syntax Trees" href="ast.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="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. 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="../index.html">Python v2.6.6 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">
<h1>31.3. <tt class="xref docutils literal"><span class="pre">symtable</span></tt> — 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. <tt class="xref docutils literal"><span class="pre">symtable</span></tt> 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>Return the toplevel <a title="symtable.SymbolTable" class="reference internal" href="#symtable.SymbolTable"><tt class="xref 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 title="compile" class="reference external" href="functions.html#compile"><tt class="xref docutils literal"><span class="pre">compile()</span></tt></a>.</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>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>.</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>Return the table’s identifier.</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>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 title="symtable.SymbolTable.get_type" class="reference internal" href="#symtable.SymbolTable.get_type"><tt class="xref docutils literal"><span class="pre">get_type()</span></tt></a> returns <tt class="docutils literal"><span class="pre">'module'</span></tt>).</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>Return the number of the first line in the block this table represents.</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>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the locals in this table can be optimized.</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>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the block is a nested class or function.</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>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the block has nested namespaces within it. These can
be obtained with <a title="symtable.SymbolTable.get_children" class="reference internal" href="#symtable.SymbolTable.get_children"><tt class="xref docutils literal"><span class="pre">get_children()</span></tt></a>.</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>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the block uses <tt class="docutils literal"><span class="pre">exec</span></tt>.</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>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the block uses a starred from-import.</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>Return a list of names of symbols in this table.</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>Lookup <em>name</em> in the table and return a <a title="symtable.Symbol" class="reference internal" href="#symtable.Symbol"><tt class="xref docutils literal"><span class="pre">Symbol</span></tt></a> instance.</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>Return a list of <a title="symtable.Symbol" class="reference internal" href="#symtable.Symbol"><tt class="xref docutils literal"><span class="pre">Symbol</span></tt></a> instances for names in the table.</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>Return a list of the nested symbol tables.</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 title="symtable.SymbolTable" class="reference internal" href="#symtable.SymbolTable"><tt class="xref 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>Return a tuple containing names of parameters to this function.</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>Return a tuple containing names of locals in this function.</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>Return a tuple containing names of globals in this function.</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>Return a tuple containing names of free variables in this function.</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 title="symtable.SymbolTable" class="reference internal" href="#symtable.SymbolTable"><tt class="xref 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>Return a tuple containing the names of methods declared in the class.</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 title="symtable.SymbolTable" class="reference internal" href="#symtable.SymbolTable"><tt class="xref 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>Return the symbol’s name.</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>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the symbol is used in its block.</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>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the symbol is created from an import statement.</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>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the symbol is a parameter.</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>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the symbol is global.</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>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the symbol is declared global with a global statement.</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>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the symbol is local to its block.</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>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the symbol is referenced in its block, but not assigned
to.</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>Return <tt class="xref docutils literal"><span class="pre">True</span></tt> if the symbol is assigned to in its block.</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="xref 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="xref 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>Return a list of namespaces bound to this name.</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>Return the namespace bound to this name. If more than one namespace is
bound, a <a title="exceptions.ValueError" class="reference external" href="exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a> is raised.</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 external" 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 external" href="#generating-symbol-tables">31.3.1. Generating Symbol Tables</a></li>
<li><a class="reference external" 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. 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 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" 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="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. Abstract Syntax Trees"
>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.6 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-2011, 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 20, 2011.
<a href="../bugs.html">Found a bug</a>?
<br />
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.6.
</div>
</body>
</html>
Copyright 2K16 - 2K18 Indonesian Hacker Rulez