| 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="prev" href="import.html" /> |
| 13 | <link rel="parent" href="import.html" /> |
| 14 | <link rel="next" href="global.html" /> |
| 15 | <meta name='aesop' content='information' /> |
| 16 | <title>6.12.1 Future statements </title> |
| 17 | </head> |
| 18 | <body> |
| 19 | <DIV CLASS="navigation"> |
| 20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 22 | <tr> |
| 23 | <td class='online-navigation'><a rel="prev" title="6.12 The import statement" |
| 24 | href="import.html"><img src='../icons/previous.png' |
| 25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 26 | <td class='online-navigation'><a rel="parent" title="6.12 The import statement" |
| 27 | href="import.html"><img src='../icons/up.png' |
| 28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 29 | <td class='online-navigation'><a rel="next" title="6.13 The global statement" |
| 30 | href="global.html"><img src='../icons/next.png' |
| 31 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 32 | <td align="center" width="100%">Python Reference Manual</td> |
| 33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 34 | href="contents.html"><img src='../icons/contents.png' |
| 35 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 36 | <td class='online-navigation'><img src='../icons/blank.png' |
| 37 | border='0' height='32' alt='' width='32' /></td> |
| 38 | <td class='online-navigation'><a rel="index" title="Index" |
| 39 | href="genindex.html"><img src='../icons/index.png' |
| 40 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 41 | </tr></table> |
| 42 | <div class='online-navigation'> |
| 43 | <b class="navlabel">Previous:</b> |
| 44 | <a class="sectref" rel="prev" href="import.html">6.12 The import statement</A> |
| 45 | <b class="navlabel">Up:</b> |
| 46 | <a class="sectref" rel="parent" href="import.html">6.12 The import statement</A> |
| 47 | <b class="navlabel">Next:</b> |
| 48 | <a class="sectref" rel="next" href="global.html">6.13 The global statement</A> |
| 49 | </div> |
| 50 | <hr /></div> |
| 51 | </DIV> |
| 52 | <!--End of Navigation Panel--> |
| 53 | |
| 54 | <H2><A NAME="SECTION0081210000000000000000"></A><A NAME="future"></A> |
| 55 | <BR> |
| 56 | 6.12.1 Future statements |
| 57 | </H2> |
| 58 | |
| 59 | <P> |
| 60 | A <i class="dfn">future statement</i><a id='l2h-556' xml:id='l2h-556'></a> is a directive to |
| 61 | the compiler that a particular module should be compiled using syntax |
| 62 | or semantics that will be available in a specified future release of |
| 63 | Python. The future statement is intended to ease migration to future |
| 64 | versions of Python that introduce incompatible changes to the |
| 65 | language. It allows use of the new features on a per-module basis |
| 66 | before the release in which the feature becomes standard. |
| 67 | |
| 68 | <P> |
| 69 | <dl><dd class="grammar"> |
| 70 | <div class="productions"> |
| 71 | <table> |
| 72 | |
| 73 | <tr> |
| 74 | <td>future_statement</td> |
| 75 | <td>::=</td> |
| 76 | <td>"from" "__future__" "import" feature ["as" name] ("," feature ["as" name])*</td></tr> |
| 77 | <tr> |
| 78 | <td></td> |
| 79 | <td></td> |
| 80 | <td><code>| "from" "__future__" "import" "(" feature ["as" name] ("," feature ["as" name])* [","] ")"</code></td></tr> |
| 81 | <tr> |
| 82 | <td>feature</td> |
| 83 | <td>::=</td> |
| 84 | <td>identifier</td></tr> |
| 85 | <tr> |
| 86 | <td>name</td> |
| 87 | <td>::=</td> |
| 88 | <td>identifier</td></tr> |
| 89 | </table> |
| 90 | </div> |
| 91 | </dd></dl> |
| 92 | |
| 93 | <P> |
| 94 | A future statement must appear near the top of the module. The only |
| 95 | lines that can appear before a future statement are: |
| 96 | |
| 97 | <P> |
| 98 | |
| 99 | <UL> |
| 100 | <LI>the module docstring (if any), |
| 101 | </LI> |
| 102 | <LI>comments, |
| 103 | </LI> |
| 104 | <LI>blank lines, and |
| 105 | </LI> |
| 106 | <LI>other future statements. |
| 107 | |
| 108 | <P> |
| 109 | </LI> |
| 110 | </UL> |
| 111 | |
| 112 | <P> |
| 113 | The features recognized by Python 2.3 are "<tt class="samp">generators</tt>", |
| 114 | "<tt class="samp">division</tt>" and "<tt class="samp">nested_scopes</tt>". "<tt class="samp">generators</tt>" and |
| 115 | "<tt class="samp">nested_scopes</tt>" are redundant in 2.3 because they are always |
| 116 | enabled. |
| 117 | |
| 118 | <P> |
| 119 | A future statement is recognized and treated specially at compile |
| 120 | time: Changes to the semantics of core constructs are often |
| 121 | implemented by generating different code. It may even be the case |
| 122 | that a new feature introduces new incompatible syntax (such as a new |
| 123 | reserved word), in which case the compiler may need to parse the |
| 124 | module differently. Such decisions cannot be pushed off until |
| 125 | runtime. |
| 126 | |
| 127 | <P> |
| 128 | For any given release, the compiler knows which feature names have been |
| 129 | defined, and raises a compile-time error if a future statement contains |
| 130 | a feature not known to it. |
| 131 | |
| 132 | <P> |
| 133 | The direct runtime semantics are the same as for any import statement: |
| 134 | there is a standard module <tt class="module">__future__</tt>, described later, and |
| 135 | it will be imported in the usual way at the time the future statement |
| 136 | is executed. |
| 137 | |
| 138 | <P> |
| 139 | The interesting runtime semantics depend on the specific feature |
| 140 | enabled by the future statement. |
| 141 | |
| 142 | <P> |
| 143 | Note that there is nothing special about the statement: |
| 144 | |
| 145 | <P> |
| 146 | <div class="verbatim"><pre> |
| 147 | import __future__ [as name] |
| 148 | </pre></div> |
| 149 | |
| 150 | <P> |
| 151 | That is not a future statement; it's an ordinary import statement with |
| 152 | no special semantics or syntax restrictions. |
| 153 | |
| 154 | <P> |
| 155 | Code compiled by an exec statement or calls to the builtin functions |
| 156 | <tt class="function">compile()</tt> and <tt class="function">execfile()</tt> that occur in a module |
| 157 | <tt class="module">M</tt> containing a future statement will, by default, use the new |
| 158 | syntax or semantics associated with the future statement. This can, |
| 159 | starting with Python 2.2 be controlled by optional arguments to |
| 160 | <tt class="function">compile()</tt> -- see the documentation of that function in the |
| 161 | library reference for details. |
| 162 | |
| 163 | <P> |
| 164 | A future statement typed at an interactive interpreter prompt will |
| 165 | take effect for the rest of the interpreter session. If an |
| 166 | interpreter is started with the <b class="programopt">-i</b> option, is passed a |
| 167 | script name to execute, and the script includes a future statement, it |
| 168 | will be in effect in the interactive session started after the script |
| 169 | is executed. |
| 170 | |
| 171 | <P> |
| 172 | |
| 173 | <DIV CLASS="navigation"> |
| 174 | <div class='online-navigation'> |
| 175 | <p></p><hr /> |
| 176 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 177 | <tr> |
| 178 | <td class='online-navigation'><a rel="prev" title="6.12 The import statement" |
| 179 | href="import.html"><img src='../icons/previous.png' |
| 180 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 181 | <td class='online-navigation'><a rel="parent" title="6.12 The import statement" |
| 182 | href="import.html"><img src='../icons/up.png' |
| 183 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 184 | <td class='online-navigation'><a rel="next" title="6.13 The global statement" |
| 185 | href="global.html"><img src='../icons/next.png' |
| 186 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 187 | <td align="center" width="100%">Python Reference Manual</td> |
| 188 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 189 | href="contents.html"><img src='../icons/contents.png' |
| 190 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 191 | <td class='online-navigation'><img src='../icons/blank.png' |
| 192 | border='0' height='32' alt='' width='32' /></td> |
| 193 | <td class='online-navigation'><a rel="index" title="Index" |
| 194 | href="genindex.html"><img src='../icons/index.png' |
| 195 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 196 | </tr></table> |
| 197 | <div class='online-navigation'> |
| 198 | <b class="navlabel">Previous:</b> |
| 199 | <a class="sectref" rel="prev" href="import.html">6.12 The import statement</A> |
| 200 | <b class="navlabel">Up:</b> |
| 201 | <a class="sectref" rel="parent" href="import.html">6.12 The import statement</A> |
| 202 | <b class="navlabel">Next:</b> |
| 203 | <a class="sectref" rel="next" href="global.html">6.13 The global statement</A> |
| 204 | </div> |
| 205 | </div> |
| 206 | <hr /> |
| 207 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 208 | </DIV> |
| 209 | <!--End of Navigation Panel--> |
| 210 | <ADDRESS> |
| 211 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 212 | </ADDRESS> |
| 213 | </BODY> |
| 214 | </HTML> |