| 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> |
| 57 | 4.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 |
| 63 | name binding operations. Each occurrence of a name in the program |
| 64 | text refers to the <i class="dfn">binding</i><a id='l2h-307' xml:id='l2h-307'></a> of that name |
| 65 | established in the innermost function block containing the use. |
| 66 | |
| 67 | <P> |
| 68 | A <i class="dfn">block</i><a id='l2h-316' xml:id='l2h-316'></a> is a piece of Python program text that is |
| 69 | executed as a unit. The following are blocks: a module, a function |
| 70 | body, and a class definition. Each command typed interactively is a |
| 71 | block. A script file (a file given as standard input to the |
| 72 | interpreter or specified on the interpreter command line the first |
| 73 | argument) is a code block. A script command (a command specified on |
| 74 | the interpreter command line with the `<strong>-c</strong>' option) is a code |
| 75 | block. The file read by the built-in function <tt class="function">execfile()</tt> |
| 76 | is 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. |
| 78 | The expression read and evaluated by the built-in function |
| 79 | <tt class="function">input()</tt> is a code block. |
| 80 | |
| 81 | <P> |
| 82 | A code block is executed in an <i class="dfn">execution |
| 83 | frame</i><a id='l2h-308' xml:id='l2h-308'></a>. A frame contains some |
| 84 | administrative information (used for debugging) and determines where |
| 85 | and how execution continues after the code block's execution has |
| 86 | completed. |
| 87 | |
| 88 | <P> |
| 89 | A <i class="dfn">scope</i><a id='l2h-317' xml:id='l2h-317'></a> defines the visibility of a name within a |
| 90 | block. If a local variable is defined in a block, its scope includes |
| 91 | that block. If the definition occurs in a function block, the scope |
| 92 | extends to any blocks contained within the defining one, unless a |
| 93 | contained block introduces a different binding for the name. The |
| 94 | scope of names defined in a class block is limited to the class block; |
| 95 | it does not extend to the code blocks of methods. |
| 96 | |
| 97 | <P> |
| 98 | When a name is used in a code block, it is resolved using the nearest |
| 99 | enclosing scope. The set of all such scopes visible to a code block |
| 100 | is called the block's <i class="dfn">environment</i><a id='l2h-318' xml:id='l2h-318'></a>. |
| 101 | |
| 102 | <P> |
| 103 | If a name is bound in a block, it is a local variable of that block. |
| 104 | If a name is bound at the module level, it is a global variable. (The |
| 105 | variables of the module code block are local and global.) If a |
| 106 | variable 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> |
| 110 | When 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 |
| 112 | refers 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 |
| 114 | raised. <tt class="exception">UnboundLocalError</tt> is a subclass of |
| 115 | <tt class="exception">NameError</tt>. |
| 116 | |
| 117 | <P> |
| 118 | The following constructs bind names: formal parameters to functions, |
| 119 | <tt class="keyword">import</tt> statements, class and function definitions (these |
| 120 | bind the class or function name in the defining block), and targets |
| 121 | that are identifiers if occurring in an assignment, <tt class="keyword">for</tt> loop |
| 122 | header, or in the second position of an <tt class="keyword">except</tt> clause |
| 123 | header. 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 |
| 125 | imported module, except those beginning with an underscore. This form |
| 126 | may only be used at the module level. |
| 127 | |
| 128 | <P> |
| 129 | A target occurring in a <tt class="keyword">del</tt> statement is also considered bound |
| 130 | for this purpose (though the actual semantics are to unbind the |
| 131 | name). It is illegal to unbind a name that is referenced by an |
| 132 | enclosing scope; the compiler will report a <tt class="exception">SyntaxError</tt>. |
| 133 | |
| 134 | <P> |
| 135 | Each assignment or import statement occurs within a block defined by a |
| 136 | class or function definition or at the module level (the top-level |
| 137 | code block). |
| 138 | |
| 139 | <P> |
| 140 | If a name binding operation occurs anywhere within a code block, all |
| 141 | uses of the name within the block are treated as references to the |
| 142 | current block. This can lead to errors when a name is used within a |
| 143 | block before it is bound. |
| 144 | This rule is subtle. Python lacks declarations and allows |
| 145 | name binding operations to occur anywhere within a code block. The |
| 146 | local variables of a code block can be determined by scanning the |
| 147 | entire text of the block for name binding operations. |
| 148 | |
| 149 | <P> |
| 150 | If the global statement occurs within a block, all uses of the name |
| 151 | specified in the statement refer to the binding of that name in the |
| 152 | top-level namespace. Names are resolved in the top-level namespace by |
| 153 | searching the global namespace, i.e. the namespace of the module |
| 154 | containing the code block, and the builtin namespace, the namespace of |
| 155 | the module <tt class="module">__builtin__</tt>. The global namespace is searched |
| 156 | first. If the name is not found there, the builtin namespace is |
| 157 | searched. The global statement must precede all uses of the name. |
| 158 | |
| 159 | <P> |
| 160 | The built-in namespace associated with the execution of a code block |
| 161 | is actually found by looking up the name <code>__builtins__</code> in its |
| 162 | global namespace; this should be a dictionary or a module (in the |
| 163 | latter 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 |
| 166 | execution<a id='l2h-313' xml:id='l2h-313'></a> mode is in effect. |
| 167 | |
| 168 | <P> |
| 169 | The namespace for a module is automatically created the first time a |
| 170 | module 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> |
| 174 | The global statement has the same scope as a name binding operation |
| 175 | in the same block. If the nearest enclosing scope for a free variable |
| 176 | contains a global statement, the free variable is treated as a global. |
| 177 | |
| 178 | <P> |
| 179 | A class definition is an executable statement that may use and define |
| 180 | names. These references follow the normal rules for name resolution. |
| 181 | The namespace of the class definition becomes the attribute dictionary |
| 182 | of the class. Names defined at the class scope are not visible in |
| 183 | methods. |
| 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> |
| 236 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 237 | </ADDRESS> |
| 238 | </BODY> |
| 239 | </HTML> |