Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / ref / naming.html
CommitLineData
920dae64
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="ref.css" type='text/css' />
5<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
6<link rel='start' href='../index.html' title='Python Documentation Index' />
7<link rel="first" href="ref.html" title='Python Reference Manual' />
8<link rel='contents' href='contents.html' title="Contents" />
9<link rel='index' href='genindex.html' title='Index' />
10<link rel='last' href='about.html' title='About this document...' />
11<link rel='help' href='about.html' title='About this document...' />
12<link rel="next" href="exceptions.html" />
13<link rel="prev" href="execmodel.html" />
14<link rel="parent" href="execmodel.html" />
15<link rel="next" href="dynamic-features.html" />
16<meta name='aesop' content='information' />
17<title>4.1 Naming and binding </title>
18</head>
19<body>
20<DIV CLASS="navigation">
21<div id='top-navigation-panel' xml:id='top-navigation-panel'>
22<table align="center" width="100%" cellpadding="0" cellspacing="2">
23<tr>
24<td class='online-navigation'><a rel="prev" title="4. Execution model"
25 href="execmodel.html"><img src='../icons/previous.png'
26 border='0' height='32' alt='Previous Page' width='32' /></A></td>
27<td class='online-navigation'><a rel="parent" title="4. Execution model"
28 href="execmodel.html"><img src='../icons/up.png'
29 border='0' height='32' alt='Up One Level' width='32' /></A></td>
30<td class='online-navigation'><a rel="next" title="4.1.1 Interaction with dynamic"
31 href="dynamic-features.html"><img src='../icons/next.png'
32 border='0' height='32' alt='Next Page' width='32' /></A></td>
33<td align="center" width="100%">Python Reference Manual</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="contents.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><img src='../icons/blank.png'
38 border='0' height='32' alt='' width='32' /></td>
39<td class='online-navigation'><a rel="index" title="Index"
40 href="genindex.html"><img src='../icons/index.png'
41 border='0' height='32' alt='Index' width='32' /></A></td>
42</tr></table>
43<div class='online-navigation'>
44<b class="navlabel">Previous:</b>
45<a class="sectref" rel="prev" href="execmodel.html">4. Execution model</A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="execmodel.html">4. Execution model</A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="dynamic-features.html">4.1.1 Interaction with dynamic</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H1><A NAME="SECTION006100000000000000000"></A><A NAME="naming"></A>
56<BR>
574.1 Naming and binding
58</H1>
59<a id='l2h-306' xml:id='l2h-306'></a><a id='l2h-314' xml:id='l2h-314'></a>
60
61<P>
62<i class="dfn">Names</i><a id='l2h-315' xml:id='l2h-315'></a> refer to objects. Names are introduced by
63name binding operations. Each occurrence of a name in the program
64text refers to the <i class="dfn">binding</i><a id='l2h-307' xml:id='l2h-307'></a> of that name
65established in the innermost function block containing the use.
66
67<P>
68A <i class="dfn">block</i><a id='l2h-316' xml:id='l2h-316'></a> is a piece of Python program text that is
69executed as a unit. The following are blocks: a module, a function
70body, and a class definition. Each command typed interactively is a
71block. A script file (a file given as standard input to the
72interpreter or specified on the interpreter command line the first
73argument) is a code block. A script command (a command specified on
74the interpreter command line with the `<strong>-c</strong>' option) is a code
75block. The file read by the built-in function <tt class="function">execfile()</tt>
76is a code block. The string argument passed to the built-in function
77<tt class="function">eval()</tt> and to the <tt class="keyword">exec</tt> statement is a code block.
78The expression read and evaluated by the built-in function
79<tt class="function">input()</tt> is a code block.
80
81<P>
82A code block is executed in an <i class="dfn">execution
83frame</i><a id='l2h-308' xml:id='l2h-308'></a>. A frame contains some
84administrative information (used for debugging) and determines where
85and how execution continues after the code block's execution has
86completed.
87
88<P>
89A <i class="dfn">scope</i><a id='l2h-317' xml:id='l2h-317'></a> defines the visibility of a name within a
90block. If a local variable is defined in a block, its scope includes
91that block. If the definition occurs in a function block, the scope
92extends to any blocks contained within the defining one, unless a
93contained block introduces a different binding for the name. The
94scope of names defined in a class block is limited to the class block;
95it does not extend to the code blocks of methods.
96
97<P>
98When a name is used in a code block, it is resolved using the nearest
99enclosing scope. The set of all such scopes visible to a code block
100is called the block's <i class="dfn">environment</i><a id='l2h-318' xml:id='l2h-318'></a>.
101
102<P>
103If a name is bound in a block, it is a local variable of that block.
104If a name is bound at the module level, it is a global variable. (The
105variables of the module code block are local and global.) If a
106variable is used in a code block but not defined there, it is a
107<i class="dfn">free variable</i><a id='l2h-309' xml:id='l2h-309'></a>.
108
109<P>
110When a name is not found at all, a
111<tt class="exception">NameError</tt><a id='l2h-311' xml:id='l2h-311'></a> exception is raised. If the name
112refers to a local variable that has not been bound, a
113<tt class="exception">UnboundLocalError</tt><a id='l2h-319' xml:id='l2h-319'></a> exception is
114raised. <tt class="exception">UnboundLocalError</tt> is a subclass of
115<tt class="exception">NameError</tt>.
116
117<P>
118The following constructs bind names: formal parameters to functions,
119<tt class="keyword">import</tt> statements, class and function definitions (these
120bind the class or function name in the defining block), and targets
121that are identifiers if occurring in an assignment, <tt class="keyword">for</tt> loop
122header, or in the second position of an <tt class="keyword">except</tt> clause
123header. The <tt class="keyword">import</tt> statement of the form ``"<tt class="samp">from
124...import *</tt>"''<a id='l2h-312' xml:id='l2h-312'></a> binds all names defined in the
125imported module, except those beginning with an underscore. This form
126may only be used at the module level.
127
128<P>
129A target occurring in a <tt class="keyword">del</tt> statement is also considered bound
130for this purpose (though the actual semantics are to unbind the
131name). It is illegal to unbind a name that is referenced by an
132enclosing scope; the compiler will report a <tt class="exception">SyntaxError</tt>.
133
134<P>
135Each assignment or import statement occurs within a block defined by a
136class or function definition or at the module level (the top-level
137code block).
138
139<P>
140If a name binding operation occurs anywhere within a code block, all
141uses of the name within the block are treated as references to the
142current block. This can lead to errors when a name is used within a
143block before it is bound.
144This rule is subtle. Python lacks declarations and allows
145name binding operations to occur anywhere within a code block. The
146local variables of a code block can be determined by scanning the
147entire text of the block for name binding operations.
148
149<P>
150If the global statement occurs within a block, all uses of the name
151specified in the statement refer to the binding of that name in the
152top-level namespace. Names are resolved in the top-level namespace by
153searching the global namespace, i.e. the namespace of the module
154containing the code block, and the builtin namespace, the namespace of
155the module <tt class="module">__builtin__</tt>. The global namespace is searched
156first. If the name is not found there, the builtin namespace is
157searched. The global statement must precede all uses of the name.
158
159<P>
160The built-in namespace associated with the execution of a code block
161is actually found by looking up the name <code>__builtins__</code> in its
162global namespace; this should be a dictionary or a module (in the
163latter case the module's dictionary is used). Normally, the
164<code>__builtins__</code> namespace is the dictionary of the built-in module
165<tt class="module">__builtin__</tt> (note: no `s'). If it isn't, restricted
166execution<a id='l2h-313' xml:id='l2h-313'></a> mode is in effect.
167
168<P>
169The namespace for a module is automatically created the first time a
170module is imported. The main module for a script is always called
171<tt class="module">__main__</tt><a id='l2h-320' xml:id='l2h-320'></a>.
172
173<P>
174The global statement has the same scope as a name binding operation
175in the same block. If the nearest enclosing scope for a free variable
176contains a global statement, the free variable is treated as a global.
177
178<P>
179A class definition is an executable statement that may use and define
180names. These references follow the normal rules for name resolution.
181The namespace of the class definition becomes the attribute dictionary
182of the class. Names defined at the class scope are not visible in
183methods.
184
185<P>
186
187<p><br /></p><hr class='online-navigation' />
188<div class='online-navigation'>
189<!--Table of Child-Links-->
190<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>
191
192<UL CLASS="ChildLinks">
193<LI><A href="dynamic-features.html">4.1.1 Interaction with dynamic features</a>
194</ul>
195<!--End of Table of Child-Links-->
196</div>
197
198<DIV CLASS="navigation">
199<div class='online-navigation'>
200<p></p><hr />
201<table align="center" width="100%" cellpadding="0" cellspacing="2">
202<tr>
203<td class='online-navigation'><a rel="prev" title="4. Execution model"
204 href="execmodel.html"><img src='../icons/previous.png'
205 border='0' height='32' alt='Previous Page' width='32' /></A></td>
206<td class='online-navigation'><a rel="parent" title="4. Execution model"
207 href="execmodel.html"><img src='../icons/up.png'
208 border='0' height='32' alt='Up One Level' width='32' /></A></td>
209<td class='online-navigation'><a rel="next" title="4.1.1 Interaction with dynamic"
210 href="dynamic-features.html"><img src='../icons/next.png'
211 border='0' height='32' alt='Next Page' width='32' /></A></td>
212<td align="center" width="100%">Python Reference Manual</td>
213<td class='online-navigation'><a rel="contents" title="Table of Contents"
214 href="contents.html"><img src='../icons/contents.png'
215 border='0' height='32' alt='Contents' width='32' /></A></td>
216<td class='online-navigation'><img src='../icons/blank.png'
217 border='0' height='32' alt='' width='32' /></td>
218<td class='online-navigation'><a rel="index" title="Index"
219 href="genindex.html"><img src='../icons/index.png'
220 border='0' height='32' alt='Index' width='32' /></A></td>
221</tr></table>
222<div class='online-navigation'>
223<b class="navlabel">Previous:</b>
224<a class="sectref" rel="prev" href="execmodel.html">4. Execution model</A>
225<b class="navlabel">Up:</b>
226<a class="sectref" rel="parent" href="execmodel.html">4. Execution model</A>
227<b class="navlabel">Next:</b>
228<a class="sectref" rel="next" href="dynamic-features.html">4.1.1 Interaction with dynamic</A>
229</div>
230</div>
231<hr />
232<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
233</DIV>
234<!--End of Navigation Panel-->
235<ADDRESS>
236See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
237</ADDRESS>
238</BODY>
239</HTML>