Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / html / python / ref / naming.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="ref.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="ref.html" title='Python Reference Manual' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="exceptions.html" />
<link rel="prev" href="execmodel.html" />
<link rel="parent" href="execmodel.html" />
<link rel="next" href="dynamic-features.html" />
<meta name='aesop' content='information' />
<title>4.1 Naming and binding </title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="4. Execution model"
href="execmodel.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="4. Execution model"
href="execmodel.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="4.1.1 Interaction with dynamic"
href="dynamic-features.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Reference Manual</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='../icons/blank.png'
border='0' height='32' alt='' width='32' /></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="execmodel.html">4. Execution model</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="execmodel.html">4. Execution model</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="dynamic-features.html">4.1.1 Interaction with dynamic</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION006100000000000000000"></A><A NAME="naming"></A>
<BR>
4.1 Naming and binding
</H1>
<a id='l2h-306' xml:id='l2h-306'></a><a id='l2h-314' xml:id='l2h-314'></a>
<P>
<i class="dfn">Names</i><a id='l2h-315' xml:id='l2h-315'></a> refer to objects. Names are introduced by
name binding operations. Each occurrence of a name in the program
text refers to the <i class="dfn">binding</i><a id='l2h-307' xml:id='l2h-307'></a> of that name
established in the innermost function block containing the use.
<P>
A <i class="dfn">block</i><a id='l2h-316' xml:id='l2h-316'></a> is a piece of Python program text that is
executed as a unit. The following are blocks: a module, a function
body, and a class definition. Each command typed interactively is a
block. A script file (a file given as standard input to the
interpreter or specified on the interpreter command line the first
argument) is a code block. A script command (a command specified on
the interpreter command line with the `<strong>-c</strong>' option) is a code
block. The file read by the built-in function <tt class="function">execfile()</tt>
is a code block. The string argument passed to the built-in function
<tt class="function">eval()</tt> and to the <tt class="keyword">exec</tt> statement is a code block.
The expression read and evaluated by the built-in function
<tt class="function">input()</tt> is a code block.
<P>
A code block is executed in an <i class="dfn">execution
frame</i><a id='l2h-308' xml:id='l2h-308'></a>. A frame contains some
administrative information (used for debugging) and determines where
and how execution continues after the code block's execution has
completed.
<P>
A <i class="dfn">scope</i><a id='l2h-317' xml:id='l2h-317'></a> defines the visibility of a name within a
block. If a local variable is defined in a block, its scope includes
that block. If the definition occurs in a function block, the scope
extends to any blocks contained within the defining one, unless a
contained block introduces a different binding for the name. The
scope of names defined in a class block is limited to the class block;
it does not extend to the code blocks of methods.
<P>
When a name is used in a code block, it is resolved using the nearest
enclosing scope. The set of all such scopes visible to a code block
is called the block's <i class="dfn">environment</i><a id='l2h-318' xml:id='l2h-318'></a>.
<P>
If a name is bound in a block, it is a local variable of that block.
If a name is bound at the module level, it is a global variable. (The
variables of the module code block are local and global.) If a
variable is used in a code block but not defined there, it is a
<i class="dfn">free variable</i><a id='l2h-309' xml:id='l2h-309'></a>.
<P>
When a name is not found at all, a
<tt class="exception">NameError</tt><a id='l2h-311' xml:id='l2h-311'></a> exception is raised. If the name
refers to a local variable that has not been bound, a
<tt class="exception">UnboundLocalError</tt><a id='l2h-319' xml:id='l2h-319'></a> exception is
raised. <tt class="exception">UnboundLocalError</tt> is a subclass of
<tt class="exception">NameError</tt>.
<P>
The following constructs bind names: formal parameters to functions,
<tt class="keyword">import</tt> statements, class and function definitions (these
bind the class or function name in the defining block), and targets
that are identifiers if occurring in an assignment, <tt class="keyword">for</tt> loop
header, or in the second position of an <tt class="keyword">except</tt> clause
header. The <tt class="keyword">import</tt> statement of the form ``"<tt class="samp">from
...import *</tt>"''<a id='l2h-312' xml:id='l2h-312'></a> binds all names defined in the
imported module, except those beginning with an underscore. This form
may only be used at the module level.
<P>
A target occurring in a <tt class="keyword">del</tt> statement is also considered bound
for this purpose (though the actual semantics are to unbind the
name). It is illegal to unbind a name that is referenced by an
enclosing scope; the compiler will report a <tt class="exception">SyntaxError</tt>.
<P>
Each assignment or import statement occurs within a block defined by a
class or function definition or at the module level (the top-level
code block).
<P>
If a name binding operation occurs anywhere within a code block, all
uses of the name within the block are treated as references to the
current block. This can lead to errors when a name is used within a
block before it is bound.
This rule is subtle. Python lacks declarations and allows
name binding operations to occur anywhere within a code block. The
local variables of a code block can be determined by scanning the
entire text of the block for name binding operations.
<P>
If the global statement occurs within a block, all uses of the name
specified in the statement refer to the binding of that name in the
top-level namespace. Names are resolved in the top-level namespace by
searching the global namespace, i.e. the namespace of the module
containing the code block, and the builtin namespace, the namespace of
the module <tt class="module">__builtin__</tt>. The global namespace is searched
first. If the name is not found there, the builtin namespace is
searched. The global statement must precede all uses of the name.
<P>
The built-in namespace associated with the execution of a code block
is actually found by looking up the name <code>__builtins__</code> in its
global namespace; this should be a dictionary or a module (in the
latter case the module's dictionary is used). Normally, the
<code>__builtins__</code> namespace is the dictionary of the built-in module
<tt class="module">__builtin__</tt> (note: no `s'). If it isn't, restricted
execution<a id='l2h-313' xml:id='l2h-313'></a> mode is in effect.
<P>
The namespace for a module is automatically created the first time a
module is imported. The main module for a script is always called
<tt class="module">__main__</tt><a id='l2h-320' xml:id='l2h-320'></a>.
<P>
The global statement has the same scope as a name binding operation
in the same block. If the nearest enclosing scope for a free variable
contains a global statement, the free variable is treated as a global.
<P>
A class definition is an executable statement that may use and define
names. These references follow the normal rules for name resolution.
The namespace of the class definition becomes the attribute dictionary
of the class. Names defined at the class scope are not visible in
methods.
<P>
<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>
<UL CLASS="ChildLinks">
<LI><A href="dynamic-features.html">4.1.1 Interaction with dynamic features</a>
</ul>
<!--End of Table of Child-Links-->
</div>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="4. Execution model"
href="execmodel.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="4. Execution model"
href="execmodel.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="4.1.1 Interaction with dynamic"
href="dynamic-features.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Reference Manual</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='../icons/blank.png'
border='0' height='32' alt='' width='32' /></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="execmodel.html">4. Execution model</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="execmodel.html">4. Execution model</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="dynamic-features.html">4.1.1 Interaction with dynamic</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>