blob: a2359780d9bff14f479af5f22aa218915133c1e0 [file] [log] [blame]
Fred Drake39778b72001-03-23 16:20:46 +00001\chapter{Future statements and nested scopes \label{futures}}
2\sectionauthor{Jeremy Hylton}{jeremy@alum.mit.edu}
3
Jeremy Hylton324cc6e2001-03-23 15:29:54 +00004
5The semantics of Python's static scoping will change in version 2.2 to
6support resolution of unbound local names in enclosing functions'
7namespaces. The new semantics will be available in Python 2.1 through
Fred Drake39778b72001-03-23 16:20:46 +00008the use of a ``future'' statement. This appendix documents these two
Jeremy Hylton324cc6e2001-03-23 15:29:54 +00009features for Python 2.1; it will be removed in Python 2.2 and the
10features will be documented in the main sections of this manual.
11
Jeremy Hylton324cc6e2001-03-23 15:29:54 +000012
Fred Drake39778b72001-03-23 16:20:46 +000013\section{Future statements \label{future-statements}}
14
15A \dfn{future statement}\indexii{future}{statement} is a directive to
16the compiler that a particular module should be compiled using syntax
17or semantics that will be available in a specified future release of
18Python. The future statement is intended to ease migration to future
19versions of Python that introduce incompatible changes to the
20language. It allows use of the new features on a per-module basis
21before the release in which the feature becomes standard.
Jeremy Hylton324cc6e2001-03-23 15:29:54 +000022
23\begin{verbatim}
24future_statement: "from" "__future__" "import" feature ["as" name]
25 ("," feature ["as" name])*
26
27feature: identifier
28name: identifier
29\end{verbatim}
30
31A future statement must appear near the top of the module. The only
32lines that can appear before a future statement are:
33
34\begin{itemize}
35
36\item the module docstring (if any),
37\item comments,
38\item blank lines, and
39\item other future statements.
40
41\end{itemize}
42
43The only feature recognized by Python 2.1 is \samp{nested_scopes}.
44
45A future statement is recognized and treated specially at compile time:
46Changes to the semantics of core constructs are often implemented by
47generating different code. It may even be the case that a new feature
48introduces new incompatible syntax (such as a new reserved word), in
49which case the compiler may need to parse the module differently. Such
50decisions cannot be pushed off until runtime.
51
52For any given release, the compiler knows which feature names have been
53defined, and raises a compile-time error if a future statement contains
54a feature not known to it.
55
56The direct runtime semantics are the same as for any import statement:
Fred Drake39778b72001-03-23 16:20:46 +000057there is a standard module \module{__future__}, described later, and
Jeremy Hylton324cc6e2001-03-23 15:29:54 +000058it will be imported in the usual way at the time the future statement
59is executed.
60
61The interesting runtime semantics depend on the specific feature
62enabled by the future statement.
63
64Note that there is nothing special about the statement:
65
66\begin{verbatim}
67import __future__ [as name]
68\end{verbatim}
69
Fred Drake39778b72001-03-23 16:20:46 +000070That is not a future statement; it's an ordinary import statement with
Jeremy Hylton324cc6e2001-03-23 15:29:54 +000071no special semantics or syntax restrictions.
72
73Code compiled by an exec statement or calls to the builtin functions
Fred Drake39778b72001-03-23 16:20:46 +000074\function{compile()} and \function{execfile()} that occur in a module
75\module{M} containing a future statement will use the new syntax or
76semantics associated with the future statement.
Jeremy Hylton324cc6e2001-03-23 15:29:54 +000077
78A future statement typed at an interactive interpreter prompt will
79take effect for the rest of the interpreter session. If an
Fred Drake39778b72001-03-23 16:20:46 +000080interpreter is started with the \programopt{-i} option, is passed a
Jeremy Hylton324cc6e2001-03-23 15:29:54 +000081script name to execute, and the script includes a future statement, it
82will be in effect in the interactive session started after the script
83is executed.
84
85\section{\module{__future__} ---
86 Future statement definitions}
87
Fred Drake39778b72001-03-23 16:20:46 +000088\declaremodule[future]{standard}{__future__}
Jeremy Hylton324cc6e2001-03-23 15:29:54 +000089\modulesynopsis{Future statement definitions}
90
Fred Drake39778b72001-03-23 16:20:46 +000091\module{__future__} is a real module, and serves three purposes:
Jeremy Hylton324cc6e2001-03-23 15:29:54 +000092
93\begin{itemize}
94
95\item To avoid confusing existing tools that analyze import statements
96 and expect to find the modules they're importing.
97
98\item To ensure that future_statements run under releases prior to 2.1
99 at least yield runtime exceptions (the import of
Fred Drake39778b72001-03-23 16:20:46 +0000100 \module{__future__} will fail, because there was no module of
Jeremy Hylton324cc6e2001-03-23 15:29:54 +0000101 that name prior to 2.1).
102
103\item To document when incompatible changes were introduced, and when they
104 will be --- or were --- made mandatory. This is a form of executable
105 documentation, and can be inspected programatically via importing
Fred Drake39778b72001-03-23 16:20:46 +0000106 \module{__future__} and examining its contents.
Jeremy Hylton324cc6e2001-03-23 15:29:54 +0000107
108\end{itemize}
109
110Each statment in \file{__future__.py} is of the form:
111
112\begin{verbatim}
113FeatureName = "_Feature(" OptionalRelease "," MandatoryRelease ")"
114\end{verbatim}
115
Fred Drake39778b72001-03-23 16:20:46 +0000116where, normally, OptionalRelease is less then MandatoryRelease, and
117both are 5-tuples of the same form as \code{sys.version_info}:
Jeremy Hylton324cc6e2001-03-23 15:29:54 +0000118
119\begin{verbatim}
120 (PY_MAJOR_VERSION, # the 2 in 2.1.0a3; an int
121 PY_MINOR_VERSION, # the 1; an int
122 PY_MICRO_VERSION, # the 0; an int
123 PY_RELEASE_LEVEL, # "alpha", "beta", "candidate" or "final"; string
124 PY_RELEASE_SERIAL # the 3; an int
125 )
126\end{verbatim}
127
128OptionalRelease records the first release in which the feature was
129accepted.
130
131In the case of MandatoryReleases that have not yet occurred,
132MandatoryRelease predicts the release in which the feature will become
133part of the language.
134
135Else MandatoryRelease records when the feature became part of the
136language; in releases at or after that, modules no longer need a
137future statement to use the feature in question, but may continue to
138use such imports.
139
Fred Drake39778b72001-03-23 16:20:46 +0000140MandatoryRelease may also be \code{None}, meaning that a planned
141feature got dropped.
Jeremy Hylton324cc6e2001-03-23 15:29:54 +0000142
143Instances of class \class{_Feature} have two corresponding methods,
Fred Drake39778b72001-03-23 16:20:46 +0000144\method{getOptionalRelease()} and \method{getMandatoryRelease()}.
Jeremy Hylton324cc6e2001-03-23 15:29:54 +0000145
Fred Drake39778b72001-03-23 16:20:46 +0000146No feature description will ever be deleted from \module{__future__}.
Jeremy Hylton324cc6e2001-03-23 15:29:54 +0000147
Fred Drake39778b72001-03-23 16:20:46 +0000148
149\section{Nested scopes \label{nested-scopes}}
150
Jeremy Hylton324cc6e2001-03-23 15:29:54 +0000151\indexii{nested}{scopes}
152
153Nested scopes are left as an exercise for the reader.